Otherwise, let a(2) be the greatest k such that p(r)-f(k) <= r, so that a(2) = floor[g(p(r)-r)]. If p(r)-f(a(2)) = r, the algorithm terminates.

Otherwise, let a(3) be the least k such that p(r)-f(a(2))+f(k) >= r, so that a(3) = floor[g(r-p(r)+f(a(2)))]. If p(r)-f(a(2))+f(a(3)) = r, the algorithm terminates.

Otherwise, the algorithm continues inductively:

If n is odd, let a(n+1) be the greatest k such that p(r)-f(a(2))+f(a(3))-...+f(a(n))-f(k) <= r, so that a(n+1) = floor[g(p(r)-r-f(a(2))+f(a(3))-...-f(a(n)))]. If p(r)-f(a(2))+ f(a(3))- ...-f(a(n)) = r, the algorithm terminates.