Algorithm Search⌘K

Proofs: Burnikel-Ziegler division

These are the human proofs of Burnikel-Ziegler division, at full length.

Burnikel-Ziegler division returns the quotient and the remainder full

This proof shows: correctness, termination.

Let β=2w\beta = 2^w. A number XX of kk limbs is normalized if βk/2≤X<βk\beta^k/2 \le X < \beta^k.

Termination. DIV2N1N on nn limbs either stops at line L1 or calls DIV3N2N twice with h=n/2h = n/2, and DIV3N2N calls DIV2N1N at most once with hh limbs. So the size halves every two levels and the recursion has depth at most 2log⁡2n2 \log_2 n. The loop of line L9 runs at most twice by Lemma 2. SCHOOLBOOK-DIVREM terminates (Knuth, Algorithm D).

Lemma 1 (trial quotient). Let b=βhb = \beta^h, B=B1b+B2B = B_1 b + B_2 with 0≤B2<b0 \le B_2 < b and b/2≤B1<bb/2 \le B_1 < b, and A=A1b2+A2b+A3A = A_1 b^2 + A_2 b + A_3 with 0≤A2,A3<b0 \le A_2, A_3 < b and A<bBA < bB. Let Q∗=⌊A/B⌋Q^* = \lfloor A/B \rfloor and Q^=min⁡(⌊(A1b+A2)/B1⌋,b−1)\hat Q = \min(\lfloor (A_1 b + A_2)/B_1 \rfloor, b - 1). Then Q∗<bQ^* < b and Q∗≤Q^≤Q∗+2Q^* \le \hat Q \le Q^* + 2.

Proof. Q∗<bQ^* < b since A<bBA < bB. Q^≥Q∗\hat Q \ge Q^*: if Q^=b−1\hat Q = b - 1 this holds. Otherwise Q^=⌊(A1b+A2)/B1⌋\hat Q = \lfloor (A_1 b + A_2)/B_1 \rfloor, so A1b+A2−Q^B1≤B1−1A_1 b + A_2 - \hat Q B_1 \le B_1 - 1, and then

A−Q^B≤A−Q^B1b=(A1b+A2−Q^B1)b+A3≤(B1−1)b+A3<B1b≤B,A - \hat Q B \le A - \hat Q B_1 b = (A_1 b + A_2 - \hat Q B_1) b + A_3 \le (B_1 - 1)b + A_3 < B_1 b \le B,

so A−(Q^+1)B<0A - (\hat Q + 1)B < 0 and Q∗≤Q^Q^* \le \hat Q. Q^≤Q∗+2\hat Q \le Q^* + 2: if B2=0B_2 = 0, then B=B1bB = B_1 b and ⌊(A1b+A2)/B1⌋=⌊(A−A3)/B⌋≤Q∗\lfloor (A_1 b + A_2)/B_1 \rfloor = \lfloor (A - A_3)/B \rfloor \le Q^*, so Q^=Q∗\hat Q = Q^*. Otherwise B>B1b≥bB > B_1 b \ge b, and B1b>B−b>0B_1 b > B - b > 0 since B<(B1+1)bB < (B_1 + 1)b. Suppose Q^≥Q∗+3\hat Q \ge Q^* + 3. Since Q^≤(A1b+A2)/B1=(A−A3)/(B1b)≤A/(B1b)<A/(B−b)\hat Q \le (A_1 b + A_2)/B_1 = (A - A_3)/(B_1 b) \le A/(B_1 b) < A/(B - b) and Q∗>A/B−1Q^* > A/B - 1,

3≤Q^−Q∗<AB−b−AB+1=AB⋅bB−b+1,3 \le \hat Q - Q^* < \frac{A}{B - b} - \frac{A}{B} + 1 = \frac{A}{B} \cdot \frac{b}{B - b} + 1 ,

so A/B>2(B−b)/b≥2(B1−1)A/B > 2(B - b)/b \ge 2(B_1 - 1), using B≥B1bB \ge B_1 b. As 2(B1−1)2(B_1 - 1) is an integer, Q∗=⌊A/B⌋≥2(B1−1)Q^* = \lfloor A/B \rfloor \ge 2(B_1 - 1). With Q^≤b−1\hat Q \le b - 1 this gives 2(B1−1)≤Q∗≤Q^−3≤b−42(B_1 - 1) \le Q^* \le \hat Q - 3 \le b - 4, that is B1≤b/2−1B_1 \le b/2 - 1, which contradicts B1≥b/2B_1 \ge b/2. □\square

(This is Knuth's Theorems A and B of Section 4.3.1, with base b=βhb = \beta^h and a divisor of two bb-digits.)

Lemma 2 (DIV3N2N). Under the hypotheses of Lemma 1, and provided the call of line L6 returns the correct quotient and remainder, DIV3N2N returns (Q∗,A−Q∗B)(Q^*, A - Q^* B), and line L9 runs at most twice.

Proof. Since A<bBA < bB, A1≤B1A_1 \le B_1: otherwise A≥(B1+1)b2>bBA \ge (B_1 + 1) b^2 > bB. If A1<B1A_1 < B_1, then A1b+A2<B1bA_1 b + A_2 < B_1 b, which is the precondition of DIV2N1N with the normalized hh-limb divisor B1B_1, and line L6 returns Q=⌊(A1b+A2)/B1⌋≤b−1Q = \lfloor (A_1 b + A_2)/B_1 \rfloor \le b - 1 and R1=A1b+A2−QB1R_1 = A_1 b + A_2 - Q B_1. If A1=B1A_1 = B_1, then ⌊(A1b+A2)/B1⌋≥b\lfloor (A_1 b + A_2)/B_1 \rfloor \ge b, and line L7 sets Q=b−1Q = b - 1 and R1R_1 the same way. In both cases Q=Q^Q = \hat Q, and line L8 gives

R=R1b+A3−Q^B2=(A1b+A2−Q^B1)b+A3−Q^B2=A−Q^B.R = R_1 b + A_3 - \hat Q B_2 = (A_1 b + A_2 - \hat Q B_1)b + A_3 - \hat Q B_2 = A - \hat Q B .

By Lemma 1, Q^−Q∗∈{0,1,2}\hat Q - Q^* \in \{0, 1, 2\}, so R=(A−Q∗B)−(Q^−Q∗)B≥−2BR = (A - Q^* B) - (\hat Q - Q^*)B \ge -2B, and R<BR < B. Each pass of line L9 lowers QQ by 1 and raises RR by BB, keeping R=A−QBR = A - QB, and it stops at the first QQ with R≥0R \ge 0, which is Q∗Q^*, after Q^−Q∗≤2\hat Q - Q^* \le 2 passes. □\square

Theorem. If BB is a normalized nn-limb number and 0≤A<βnB0 \le A < \beta^n B, DIV2N1N returns (⌊A/B⌋,A mod B)(\lfloor A/B \rfloor, A \bmod B).

Proof. By strong induction on nn. If line L1 applies, SCHOOLBOOK-DIVREM is correct (Knuth, Algorithm D, whose trial quotients are bounded by Lemma 1 with h=1h = 1). Otherwise let h=n/2h = n/2, b=βhb = \beta^h, and A=A1b3+A2b2+A3b+A4A = A_1 b^3 + A_2 b^2 + A_3 b + A_4. The top half B1=⌊B/b⌋B_1 = \lfloor B/b \rfloor is a normalized hh-limb number, so the calls of DIV2N1N inside DIV3N2N are correct by the induction hypothesis.

First call (line L3). A′=⌊A/b⌋=A1b2+A2b+A3A' = \lfloor A/b \rfloor = A_1 b^2 + A_2 b + A_3 satisfies A′≤A/b<bBA' \le A/b < bB. By Lemma 2 it returns Q1=⌊A′/B⌋Q_1 = \lfloor A'/B \rfloor and R=A′−Q1B∈[0,B)R = A' - Q_1 B \in [0, B).

Second call (line L4). A′′=Rb+A4≤(B−1)b+b−1<bBA'' = Rb + A_4 \le (B - 1)b + b - 1 < bB, so Lemma 2 applies and returns Q2=⌊A′′/B⌋<bQ_2 = \lfloor A''/B \rfloor < b and S=A′′−Q2B∈[0,B)S = A'' - Q_2 B \in [0, B).

Result. A=A′b+A4=(Q1B+R)b+A4=Q1bB+A′′=(Q1b+Q2)B+SA = A' b + A_4 = (Q_1 B + R) b + A_4 = Q_1 b B + A'' = (Q_1 b + Q_2) B + S with 0≤S<B0 \le S < B, so by the uniqueness of division with remainder, Q1b+Q2=⌊A/B⌋Q_1 b + Q_2 = \lfloor A/B \rfloor and S=A mod BS = A \bmod B; line L5 returns them. ■\blacksquare

Back to Burnikel-Ziegler division