Algorithm Search⌘K

Proofs: Newton's method for integer roots

These are the human proofs of Newton's method for integer roots, at full length.

The integer Newton iteration stops exactly at the floor of the k-th root full

This proof shows: correctness, termination.

Let N≥0N \ge 0 and k≥1k \ge 1 be integers and s=⌊N1/k⌋s = \lfloor N^{1/k} \rfloor, the unique integer s≥0s \ge 0 with sk≤N<(s+1)ks^k \le N < (s + 1)^k.

Theorem. NEWTON-ROOT(N,k)(N, k) terminates and returns ss.

Proof. If k=1k = 1, then s=Ns = N, and if N∈{0,1}N \in \{0, 1\}, then s=Ns = N; line L1 returns NN in these cases. From now on k≥2k \ge 2 and N≥2N \ge 2, so s≥1s \ge 1.

The start. Let B≥2B \ge 2 be the binary length of NN, so 2B−1≤N<2B2^{B-1} \le N < 2^B. Then x0=2⌈B/k⌉≥2B/k>N1/k≥sx_0 = 2^{\lceil B/k \rceil} \ge 2^{B/k} > N^{1/k} \ge s. In particular x0≥1x_0 \ge 1.

Lemma 1 (one step). For an integer x≥1x \ge 1, let y=⌊((k−1)x+⌊N/xk−1⌋)/k⌋y = \lfloor ((k - 1)x + \lfloor N / x^{k-1} \rfloor) / k \rfloor, the value of line L4. Then

(a) y=⌊g(x)⌋y = \lfloor g(x) \rfloor, where g(x)=((k−1)x+N/xk−1)/kg(x) = \big((k - 1)x + N / x^{k-1}\big)/k;

(b) y≥sy \ge s;

(c) if x>sx > s, then y<xy < x;

(d) if x=sx = s, then y≥xy \ge x.

Proof of (a). Let a=(k−1)xa = (k - 1)x, an integer, and t=N/xk−1≥0t = N / x^{k-1} \ge 0. For an integer aa and a real tt, ⌊(a+⌊t⌋)/k⌋=⌊(a+t)/k⌋\lfloor (a + \lfloor t \rfloor)/k \rfloor = \lfloor (a + t)/k \rfloor: write a+⌊t⌋=qk+ρa + \lfloor t \rfloor = qk + \rho with 0≤ρ≤k−10 \le \rho \le k - 1; then (a+t)/k=q+(ρ+{t})/k(a + t)/k = q + (\rho + \{t\})/k with 0≤ρ+{t}<k0 \le \rho + \{t\} < k, so both sides equal qq.

Proof of (b). By the inequality of the arithmetic and geometric means for the kk nonnegative numbers x,…,xx, \ldots, x (k−1k - 1 times) and N/xk−1N / x^{k-1},

g(x)≥(xk−1⋅N/xk−1)1/k=N1/k.g(x) \ge \big( x^{k-1} \cdot N / x^{k-1} \big)^{1/k} = N^{1/k} .

So y=⌊g(x)⌋≥⌊N1/k⌋=sy = \lfloor g(x) \rfloor \ge \lfloor N^{1/k} \rfloor = s, since the floor is monotone.

Proof of (c). If x>sx > s, then x≥s+1x \ge s + 1, so xk≥(s+1)k>Nx^k \ge (s + 1)^k > N, hence N/xk−1<xN / x^{k-1} < x and g(x)<((k−1)x+x)/k=xg(x) < ((k - 1)x + x)/k = x. Since xx is an integer, y=⌊g(x)⌋≤g(x)<xy = \lfloor g(x) \rfloor \le g(x) < x.

Proof of (d). If x=s≥1x = s \ge 1, then xk≤Nx^k \le N, so N/xk−1≥xN / x^{k-1} \ge x and g(x)≥xg(x) \ge x. Since xx is an integer, y=⌊g(x)⌋≥xy = \lfloor g(x) \rfloor \ge x. □\square

Invariant. At each execution of line L4, xx is an integer with x≥s≥1x \ge s \ge 1. It holds for x0x_0. If the loop continues at line L6, then x←yx \leftarrow y with y≥sy \ge s by (b), and yy is an integer.

Termination. The loop continues only when y<xy < x, so the values of xx at line L4 form a strictly decreasing sequence of integers bounded below by ss. Hence there are at most x0−s+1x_0 - s + 1 executions of line L4, and the loop stops at line L5.

Result. Let xx be the value returned at line L5, so y≥xy \ge x for its step. By the invariant x≥sx \ge s. If x>sx > s, then (c) gives y<xy < x, a contradiction. So x=sx = s. Conversely, (d) shows that the loop does stop once x=sx = s is reached, which agrees with the termination argument. ■\blacksquare

Remark (the stopping test). The test must be y≥xy \ge x, not y=xy = x. For k=2k = 2 and N=s2+2sN = s^2 + 2s, the step from x=sx = s gives y=⌊(s+⌊(s2+2s)/s⌋)/2⌋=⌊(2s+2)/2⌋=s+1>xy = \lfloor (s + \lfloor (s^2 + 2s)/s \rfloor)/2 \rfloor = \lfloor (2s + 2)/2 \rfloor = s + 1 > x, so a test for equality would continue to s+1s + 1, which maps back to ⌊(s+1+⌊(s2+2s)/(s+1)⌋)/2⌋=⌊(s+1+s)/2⌋=s\lfloor (s + 1 + \lfloor (s^2 + 2s)/(s + 1) \rfloor)/2 \rfloor = \lfloor (s + 1 + s)/2 \rfloor = s, a cycle of length two.

Back to Newton's method for integer roots