Algorithm Search⌘K

Proofs: Long division

These are the human proofs of Long division, at full length.

Algorithm D computes the exact quotient and remainder full

This proof shows: correctness, termination.

Let β≥2\beta \ge 2, n≥2n \ge 2 and m≥0m \ge 0. Let U=∑i<m+nuiβiU = \sum_{i < m+n} u_i\beta^i and V=∑i<nviβiV = \sum_{i<n} v_i\beta^i with digits in [0,β−1][0, \beta - 1] and vn−1≠0v_{n-1} \ne 0.

Theorem. LONG-DIV(u,v)(u, v) terminates and returns the digits of Q=⌊U/V⌋Q = \lfloor U/V \rfloor and of R=U−QVR = U - QV, with 0≤R<V0 \le R < V.

The proof has four parts: the normalization, two facts about the trial digit, the step invariant, and the end.

1. Normalization. Let t=vn−1t = v_{n-1} and d=⌊β/(t+1)⌋≥1d = \lfloor \beta / (t + 1) \rfloor \ge 1. Then dVdV has nn digits and its top digit is at least ⌊β/2⌋\lfloor \beta/2 \rfloor.

Proof. dV<d(t+1)βn−1≤βndV < d(t + 1)\beta^{n-1} \le \beta^n, so dVdV has at most nn digits, and its top digit is ⌊dV/βn−1⌋≥dt\lfloor dV / \beta^{n-1} \rfloor \ge dt. If t+1>β/2t + 1 > \beta / 2, then d=1d = 1 and dt=t≥⌊β/2⌋dt = t \ge \lfloor \beta/2 \rfloor. Otherwise t+1≤β/2t + 1 \le \beta/2. Since d>β/(t+1)−1d > \beta/(t+1) - 1, we get dt>tβ/(t+1)−tdt > t\beta/(t+1) - t, and tβ/(t+1)−t≥β/2−1t\beta/(t+1) - t \ge \beta/2 - 1 is equivalent to β(t−1)/(2(t+1))≥t−1\beta(t - 1)/(2(t + 1)) \ge t - 1, which holds for t=1t = 1 and, for t≥2t \ge 2, is β≥2(t+1)\beta \ge 2(t + 1). So the integer dtdt is larger than β/2−1\beta/2 - 1, hence dt≥⌊β/2⌋dt \ge \lfloor \beta/2 \rfloor. □\square

Line L1 replaces UU and VV by dUdU (with m+n+1m + n + 1 digits, the top one possibly 0) and dVdV. Since dU=Q⋅dV+dRdU = Q \cdot dV + dR with 0≤dR<dV0 \le dR < dV, the quotient is unchanged and the new remainder is dRdR. From now on, UU and VV denote the normalized values, with vn−1≥⌊β/2⌋v_{n-1} \ge \lfloor \beta/2 \rfloor.

2. The trial digit. Let W=∑i=0nwiβiW = \sum_{i=0}^{n} w_i\beta^i be a number of n+1n + 1 digits with 0≤W<βV0 \le W < \beta V, and let q=⌊W/V⌋q = \lfloor W/V \rfloor, so 0≤q≤β−10 \le q \le \beta - 1. Let q^0=⌊(wnβ+wn−1)/vn−1⌋\hat{q}_0 = \lfloor (w_n\beta + w_{n-1})/v_{n-1} \rfloor and q^=min⁡(q^0,β−1)\hat{q} = \min(\hat{q}_0, \beta - 1).

(A) q^≥q\hat{q} \ge q. If q^=β−1\hat{q} = \beta - 1 this holds, since q≤β−1q \le \beta - 1. Otherwise q^vn−1≥wnβ+wn−1−vn−1+1\hat{q} v_{n-1} \ge w_n\beta + w_{n-1} - v_{n-1} + 1, and

W−q^V≤W−q^vn−1βn−1≤wnβn+wn−1βn−1+(βn−1−1)−(wnβ+wn−1−vn−1+1)βn−1=vn−1βn−1−1<V,W - \hat{q}V \le W - \hat{q}v_{n-1}\beta^{n-1} \le w_n\beta^n + w_{n-1}\beta^{n-1} + (\beta^{n-1} - 1) - (w_n\beta + w_{n-1} - v_{n-1} + 1)\beta^{n-1} = v_{n-1}\beta^{n-1} - 1 < V ,

so W/V<q^+1W/V < \hat{q} + 1 and q≤q^q \le \hat{q}. Since q^0≥q^\hat{q}_0 \ge \hat{q}, also q^0≥q\hat{q}_0 \ge q.

(B) If vn−1≥⌊β/2⌋v_{n-1} \ge \lfloor \beta/2 \rfloor, then q^≤q+2\hat{q} \le q + 2. If V=βn−1V = \beta^{n-1}, then q=⌊W/βn−1⌋=wnβ+wn−1=q^0q = \lfloor W / \beta^{n-1} \rfloor = w_n\beta + w_{n-1} = \hat{q}_0, and q≤β−1q \le \beta - 1 gives q^=q\hat{q} = q. Otherwise V>βn−1V > \beta^{n-1}. Suppose q^≥q+3\hat{q} \ge q + 3. Then q^≤q^0≤(wnβn+wn−1βn−1)/(vn−1βn−1)≤W/(vn−1βn−1)<W/(V−βn−1)\hat{q} \le \hat{q}_0 \le (w_n\beta^n + w_{n-1}\beta^{n-1})/(v_{n-1}\beta^{n-1}) \le W/(v_{n-1}\beta^{n-1}) < W/(V - \beta^{n-1}), using V<(vn−1+1)βn−1V < (v_{n-1} + 1)\beta^{n-1}. Also q>W/V−1q > W/V - 1. So

3≤q^−q<WV−βn−1−WV+1=WV⋅βn−1V−βn−1+1,3 \le \hat{q} - q < \frac{W}{V - \beta^{n-1}} - \frac{W}{V} + 1 = \frac{W}{V} \cdot \frac{\beta^{n-1}}{V - \beta^{n-1}} + 1 ,

and W/V>2(V−βn−1)/βn−1≥2(vn−1−1)W/V > 2(V - \beta^{n-1})/\beta^{n-1} \ge 2(v_{n-1} - 1). Hence q≥2(vn−1−1)q \ge 2(v_{n-1} - 1), and β−1≥q^≥q+3≥2vn−1+1\beta - 1 \ge \hat{q} \ge q + 3 \ge 2v_{n-1} + 1, so vn−1≤β/2−1<⌊β/2⌋v_{n-1} \le \beta/2 - 1 < \lfloor \beta/2 \rfloor, a contradiction. □\square

3. One step. Fix jj and let WW be the window uj,…,uj+nu_j, \ldots, u_{j+n} before step jj, with 0≤W<βV0 \le W < \beta V (this is shown in part 4). Then step jj sets qj=q=⌊W/V⌋q_j = q = \lfloor W/V \rfloor and leaves the window equal to W−qV∈[0,V)W - qV \in [0, V).

Line L3 sets q^=q^0\hat{q} = \hat{q}_0 and r^=wnβ+wn−1−q^vn−1\hat{r} = w_n\beta + w_{n-1} - \hat{q}v_{n-1}, so 0≤r^<vn−10 \le \hat{r} < v_{n-1}. Each round of line L4 lowers q^\hat{q} by 1 and raises r^\hat{r} by vn−1v_{n-1}, so the relation q^vn−1+r^=wnβ+wn−1\hat{q}v_{n-1} + \hat{r} = w_n\beta + w_{n-1} is kept.

Line L4 keeps q^≥q\hat{q} \ge q. A round runs only if q^≥β\hat{q} \ge \beta, and then q^−1≥β−1≥q\hat{q} - 1 \ge \beta - 1 \ge q; or if q^vn−2>r^β+wn−2\hat{q}v_{n-2} > \hat{r}\beta + w_{n-2}. In the second case, adding q^vn−1β=(wnβ+wn−1−r^)β\hat{q}v_{n-1}\beta = (w_n\beta + w_{n-1} - \hat{r})\beta to both sides gives q^(vn−1β+vn−2)>wnβ2+wn−1β+wn−2\hat{q}(v_{n-1}\beta + v_{n-2}) > w_n\beta^2 + w_{n-1}\beta + w_{n-2}, so q^(vn−1β+vn−2)≥wnβ2+wn−1β+wn−2+1\hat{q}(v_{n-1}\beta + v_{n-2}) \ge w_n\beta^2 + w_{n-1}\beta + w_{n-2} + 1, and

q^V≥q^(vn−1β+vn−2)βn−2≥(wnβ2+wn−1β+wn−2+1)βn−2>W.\hat{q}V \ge \hat{q}(v_{n-1}\beta + v_{n-2})\beta^{n-2} \ge (w_n\beta^2 + w_{n-1}\beta + w_{n-2} + 1)\beta^{n-2} > W .

So q<q^q < \hat{q} and q^−1≥q\hat{q} - 1 \ge q. With (A) at the start, q^≥q\hat{q} \ge q holds throughout.

Line L4 terminates. q^\hat{q} decreases by 1 in each round and stays ≥q≥0\ge q \ge 0, so there are at most q^0−q\hat{q}_0 - q rounds. With the normalization, q^0≤(vn−1β+β−1)/vn−1<β+2\hat{q}_0 \le (v_{n-1}\beta + \beta - 1)/v_{n-1} < \beta + 2 (using wn≤vn−1w_n \le v_{n-1}, which follows from W<βV<(vn−1+1)βnW < \beta V < (v_{n-1} + 1)\beta^n), and (B) gives q≥min⁡(q^0,β−1)−2q \ge \min(\hat{q}_0, \beta - 1) - 2, so the number of rounds is at most 4.

After line L4, q^≤q+1\hat{q} \le q + 1 and q^<β\hat{q} < \beta. When the loop stops, either r^≥β\hat{r} \ge \beta or (q^<β\hat{q} < \beta and q^vn−2≤r^β+wn−2\hat{q}v_{n-2} \le \hat{r}\beta + w_{n-2}). In the first case, q^<β\hat{q} < \beta as well: if q^≥β\hat{q} \ge \beta, then r^=wnβ+wn−1−q^vn−1≤vn−1β+β−1−βvn−1<β\hat{r} = w_n\beta + w_{n-1} - \hat{q}v_{n-1} \le v_{n-1}\beta + \beta - 1 - \beta v_{n-1} < \beta. And then q^vn−2≤(β−1)2<β2≤r^β+wn−2\hat{q}v_{n-2} \le (\beta - 1)^2 < \beta^2 \le \hat{r}\beta + w_{n-2}. So in both cases q^<β\hat{q} < \beta and q^vn−2≤r^β+wn−2\hat{q}v_{n-2} \le \hat{r}\beta + w_{n-2}, which as above gives q^(vn−1β+vn−2)≤wnβ2+wn−1β+wn−2\hat{q}(v_{n-1}\beta + v_{n-2}) \le w_n\beta^2 + w_{n-1}\beta + w_{n-2}. Write V=(vn−1β+vn−2)βn−2+V0V = (v_{n-1}\beta + v_{n-2})\beta^{n-2} + V_0 with 0≤V0<βn−20 \le V_0 < \beta^{n-2}. Then

W−q^V≥(wnβ2+wn−1β+wn−2)βn−2−q^(vn−1β+vn−2)βn−2−q^V0≥−q^V0>−β⋅βn−2≥−V,W - \hat{q}V \ge (w_n\beta^2 + w_{n-1}\beta + w_{n-2})\beta^{n-2} - \hat{q}(v_{n-1}\beta + v_{n-2})\beta^{n-2} - \hat{q}V_0 \ge -\hat{q}V_0 > -\beta \cdot \beta^{n-2} \ge -V ,

so W−(q^−1)V>0W - (\hat{q} - 1)V > 0, which gives q≥q^−1q \ge \hat{q} - 1.

Lines L5 to L7. Line L5 computes W−q^VW - \hat{q}V in n+1n + 1 digits with a borrow. If q^=q\hat{q} = q, then W−q^V∈[0,V)W - \hat{q}V \in [0, V) is not negative, line L6 does nothing, and qj=qq_j = q. If q^=q+1\hat{q} = q + 1, then W−q^V=(W−qV)−V∈[−V,0)W - \hat{q}V = (W - qV) - V \in [-V, 0) is negative; the digits hold W−q^V+βn+1W - \hat{q}V + \beta^{n+1} with a borrow out, and line L6 adds VV and drops the carry, which leaves W−qVW - qV, and sets qj=q^−1=qq_j = \hat{q} - 1 = q. In both cases the window ends as W−qV∈[0,V)W - qV \in [0, V), and qj=q∈[0,β−1]q_j = q \in [0, \beta - 1] is a digit.

4. The loop invariant and the end. Let U(j)U^{(j)} be the number held in u0,…,um+nu_0, \ldots, u_{m+n} before step jj, for j=m,m−1,…,0j = m, m-1, \ldots, 0, and U(−1)U^{(-1)} the number after step 0. The invariant before step jj is

U=(∑i>jqiβi)V+U(j),0≤U(j)<βj+1V.U = \Big( \sum_{i > j} q_i \beta^i \Big) V + U^{(j)}, \qquad 0 \le U^{(j)} < \beta^{j+1} V .

Before step mm, U(m)=UU^{(m)} = U and no quotient digit is set. The unnormalized dividend is below βm+n\beta^{m+n}, so U<dβm+nU < d\beta^{m+n}, and the unnormalized divisor is at least βn−1\beta^{n-1}, so βm+1V≥βm+1dβn−1=dβm+n\beta^{m+1}V \ge \beta^{m+1} d\beta^{n-1} = d\beta^{m+n}. Hence U(m)<βm+1VU^{(m)} < \beta^{m+1}V. The digits of U(j)U^{(j)} below position jj are not touched by step jj, and U(j)<βj+1VU^{(j)} < \beta^{j+1}V means that the window W=⌊U(j)/βj⌋W = \lfloor U^{(j)}/\beta^j \rfloor satisfies W<βVW < \beta V. Step jj replaces WW by W−qjVW - q_jV with qj=⌊W/V⌋q_j = \lfloor W/V \rfloor (part 3), so U(j−1)=U(j)−qjβjVU^{(j-1)} = U^{(j)} - q_j\beta^jV and 0≤U(j−1)<βjV0 \le U^{(j-1)} < \beta^j V, since U(j−1)=(W−qjV)βj+(U(j) mod βj)≤(V−1)βj+βj−1<βjVU^{(j-1)} = (W - q_jV)\beta^j + (U^{(j)} \bmod \beta^j) \le (V - 1)\beta^j + \beta^j - 1 < \beta^j V. This is the invariant for j−1j - 1.

After step 0, U=(∑i≥0qiβi)V+U(−1)U = (\sum_{i \ge 0} q_i\beta^i)V + U^{(-1)} with 0≤U(−1)<V0 \le U^{(-1)} < V, so ∑iqiβi=⌊U/V⌋=Q\sum_i q_i\beta^i = \lfloor U/V \rfloor = Q and U(−1)U^{(-1)} is the normalized remainder dRdR. Since U(−1)<V<βnU^{(-1)} < V < \beta^n, it lies in u0,…,un−1u_0, \ldots, u_{n-1}. It is divisible by dd, so the short division of line L8 is exact and returns RR. The loop of line L2 has m+1m + 1 steps and each step terminates (part 3), so the algorithm terminates. ■\blacksquare

Back to Long division