Algorithm Search⌘K

Proofs: Digit-by-digit square root

These are the human proofs of Digit-by-digit square root, at full length.

The digit-by-digit method returns the integer square root and its remainder full

This proof shows: correctness, termination.

Let β≥2\beta \ge 2 and A=∑i=02n−1aiβiA = \sum_{i=0}^{2n-1} a_i \beta^i with 0≤ai<β0 \le a_i < \beta. For 0≤j≤n0 \le j \le n let Pj=⌊A/β2(n−j)⌋P_j = \lfloor A / \beta^{2(n-j)} \rfloor, the number formed by the top 2j2j digits. Then P0=0P_0 = 0, Pn=AP_n = A, and

Pj+1=Pjβ2+gj,gj=a2k+1β+a2k,k=n−1−j,0≤gj≤β2−1.P_{j+1} = P_j \beta^2 + g_j, \qquad g_j = a_{2k+1} \beta + a_{2k}, \quad k = n - 1 - j, \qquad 0 \le g_j \le \beta^2 - 1 .

For an integer P≥0P \ge 0, the integer q≥0q \ge 0 satisfies q=⌊P⌋q = \lfloor \sqrt P \rfloor if and only if q2≤P<(q+1)2q^2 \le P < (q + 1)^2, that is, if and only if 0≤P−q2≤2q0 \le P - q^2 \le 2q.

Invariant. After jj passes of the loop of line L2, q=⌊Pj⌋q = \lfloor \sqrt{P_j} \rfloor and r=Pj−q2r = P_j - q^2, so 0≤r≤2q0 \le r \le 2q.

Initialization. Line L1 sets q=r=0q = r = 0, and ⌊P0⌋=0\lfloor \sqrt{P_0} \rfloor = 0, P0−0=0P_0 - 0 = 0.

Maintenance. Assume the invariant after jj passes, with values qq and rr. Line L3 sets ρ=rβ2+gj=Pj+1−q2β2\rho = r \beta^2 + g_j = P_{j+1} - q^2 \beta^2.

Step 1: the new root lies in a range of β\beta values. Let Q′=⌊Pj+1⌋Q' = \lfloor \sqrt{P_{j+1}} \rfloor. First, (qβ)2=q2β2≤Pjβ2≤Pj+1(q\beta)^2 = q^2 \beta^2 \le P_j \beta^2 \le P_{j+1}, so Q′≥qβQ' \ge q\beta. Second, Pj≤(q+1)2−1P_j \le (q+1)^2 - 1 by the invariant, so

Pj+1≤((q+1)2−1)β2+β2−1=(q+1)2β2−1<((q+1)β)2,P_{j+1} \le ((q+1)^2 - 1)\beta^2 + \beta^2 - 1 = (q+1)^2 \beta^2 - 1 < ((q+1)\beta)^2,

and Q′<(q+1)βQ' < (q+1)\beta. Hence Q′=qβ+x∗Q' = q\beta + x^* for a unique x∗∈{0,…,β−1}x^* \in \{0, \ldots, \beta - 1\}.

Step 2: line L4 finds x∗x^*. For each x∈{0,…,β−1}x \in \{0, \ldots, \beta - 1\},

(qβ+x)2=q2β2+(2qβ+x) x,(q\beta + x)^2 = q^2 \beta^2 + (2q\beta + x)\, x,

so (qβ+x)2≤Pj+1(q\beta + x)^2 \le P_{j+1} holds if and only if (2qβ+x) x≤ρ(2q\beta + x)\, x \le \rho. The left side (2qβ+x)x(2q\beta + x)x increases with xx, so the set of digits xx that satisfy the test is an initial segment {0,…,xmax⁡}\{0, \ldots, x_{\max}\}, which contains 0. Its largest element is the largest xx with (qβ+x)2≤Pj+1(q\beta + x)^2 \le P_{j+1}. By Step 1 this is x∗x^*: (qβ+x∗)2=Q′2≤Pj+1(q\beta + x^*)^2 = Q'^2 \le P_{j+1}, and for x∗<x≤β−1x^* < x \le \beta - 1, qβ+x>Q′q\beta + x > Q' gives (qβ+x)2>Pj+1(q\beta + x)^2 > P_{j+1}. So line L4 sets x=x∗x = x^*. (The binary search of the implementation keeps lo≤x∗≤hilo \le x^* \le hi, since the test holds at lolo and the set is an initial segment, so it ends with lo=hi=x∗lo = hi = x^*.)

Step 3: the new values. Line L6 sets q′=qβ+x∗=Q′q' = q\beta + x^* = Q', and line L5 sets

r′=ρ−(2qβ+x∗) x∗=Pj+1−q2β2−(2qβ+x∗) x∗=Pj+1−Q′2.r' = \rho - (2q\beta + x^*)\, x^* = P_{j+1} - q^2\beta^2 - (2q\beta + x^*)\, x^* = P_{j+1} - Q'^2 .

So the invariant holds after j+1j + 1 passes, and 0≤r′≤2q′0 \le r' \le 2q' follows from Q′=⌊Pj+1⌋Q' = \lfloor \sqrt{P_{j+1}} \rfloor.

Termination. The loop of line L2 runs nn times, and line L4 examines at most β\beta candidates.

Theorem. DIGIT-SQRT returns q=⌊A⌋q = \lfloor \sqrt A \rfloor and r=A−q2r = A - q^2, with 0≤r≤2q0 \le r \le 2q. The digits appended in line L6 are the nn base β\beta digits of qq, most significant first.

Proof. After nn passes the invariant gives q=⌊Pn⌋=⌊A⌋q = \lfloor \sqrt{P_n} \rfloor = \lfloor \sqrt A \rfloor and r=A−q2r = A - q^2. Each pass replaces qq by qβ+xq\beta + x with 0≤x<β0 \le x < \beta, so the values xx in order are the digits of qq, and q<βnq < \beta^n because A<β2nA < \beta^{2n}. ■\blacksquare

Back to Digit-by-digit square root