The integer Newton iteration stops exactly at the floor of the k-th root full
This proof shows: correctness, termination.
Let N≥0 and k≥1 be integers and s=⌊N1/k⌋, the unique integer s≥0 with sk≤N<(s+1)k.
Theorem. NEWTON-ROOT(N,k) terminates and returns s.
Proof. If k=1, then s=N, and if N∈{0,1}, then s=N; line L1 returns N in these cases. From now on k≥2 and N≥2, so s≥1.
The start. Let B≥2 be the binary length of N, so 2B−1≤N<2B. Then x0=2⌈B/k⌉≥2B/k>N1/k≥s. In particular x0≥1.
Lemma 1 (one step). For an integer x≥1, let y=⌊((k−1)x+⌊N/xk−1⌋)/k⌋, the value of line L4. Then
(a) y=⌊g(x)⌋, where g(x)=((k−1)x+N/xk−1)/k;
(b) y≥s;
(c) if x>s, then y<x;
(d) if x=s, then y≥x.
Proof of (a). Let a=(k−1)x, an integer, and t=N/xk−1≥0. For an integer a and a real t, ⌊(a+⌊t⌋)/k⌋=⌊(a+t)/k⌋: write a+⌊t⌋=qk+ρ with 0≤ρ≤k−1; then (a+t)/k=q+(ρ+{t})/k with 0≤ρ+{t}<k, so both sides equal q.
Proof of (b). By the inequality of the arithmetic and geometric means for the k nonnegative numbers x,…,x (k−1 times) and N/xk−1,
g(x)≥(xk−1⋅N/xk−1)1/k=N1/k. So y=⌊g(x)⌋≥⌊N1/k⌋=s, since the floor is monotone.
Proof of (c). If x>s, then x≥s+1, so xk≥(s+1)k>N, hence N/xk−1<x and g(x)<((k−1)x+x)/k=x. Since x is an integer, y=⌊g(x)⌋≤g(x)<x.
Proof of (d). If x=s≥1, then xk≤N, so N/xk−1≥x and g(x)≥x. Since x is an integer, y=⌊g(x)⌋≥x. □
Invariant. At each execution of line L4, x is an integer with x≥s≥1. It holds for x0. If the loop continues at line L6, then x←y with y≥s by (b), and y is an integer.
Termination. The loop continues only when y<x, so the values of x at line L4 form a strictly decreasing sequence of integers bounded below by s. Hence there are at most x0−s+1 executions of line L4, and the loop stops at line L5.
Result. Let x be the value returned at line L5, so y≥x for its step. By the invariant x≥s. If x>s, then (c) gives y<x, a contradiction. So x=s. Conversely, (d) shows that the loop does stop once x=s is reached, which agrees with the termination argument. ■
Remark (the stopping test). The test must be y≥x, not y=x. For k=2 and N=s2+2s, the step from x=s gives y=⌊(s+⌊(s2+2s)/s⌋)/2⌋=⌊(2s+2)/2⌋=s+1>x, so a test for equality would continue to s+1, which maps back to ⌊(s+1+⌊(s2+2s)/(s+1)⌋)/2⌋=⌊(s+1+s)/2⌋=s, a cycle of length two.