Source: http://perso.math.univ-toulouse.fr/ktanguy/files/2018/09/DM-Algo-While.pdf