Algorithm D computes the exact quotient and remainder full
This proof shows: correctness, termination.
Let β≥2, n≥2 and m≥0. Let U=∑i<m+nuiβi and V=∑i<nviβi with digits in [0,β−1] and vn−1=0.
Theorem. LONG-DIV(u,v) terminates and returns the digits of Q=⌊U/V⌋ and of R=U−QV, with 0≤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−1 and d=⌊β/(t+1)⌋≥1. Then dV has n digits and its top digit is at least ⌊β/2⌋.
Proof. dV<d(t+1)βn−1≤βn, so dV has at most n digits, and its top digit is ⌊dV/βn−1⌋≥dt. If t+1>β/2, then d=1 and dt=t≥⌊β/2⌋. Otherwise t+1≤β/2. Since d>β/(t+1)−1, we get dt>tβ/(t+1)−t, and tβ/(t+1)−t≥β/2−1 is equivalent to β(t−1)/(2(t+1))≥t−1, which holds for t=1 and, for t≥2, is β≥2(t+1). So the integer dt is larger than β/2−1, hence dt≥⌊β/2⌋. □
Line L1 replaces U and V by dU (with m+n+1 digits, the top one possibly 0) and dV. Since dU=Q⋅dV+dR with 0≤dR<dV, the quotient is unchanged and the new remainder is dR. From now on, U and V denote the normalized values, with vn−1≥⌊β/2⌋.
2. The trial digit. Let W=∑i=0nwiβi be a number of n+1 digits with 0≤W<βV, and let q=⌊W/V⌋, so 0≤q≤β−1. Let q^0=⌊(wnβ+wn−1)/vn−1⌋ and q^=min(q^0,β−1).
(A) q^≥q. If q^=β−1 this holds, since q≤β−1. Otherwise q^vn−1≥wnβ+wn−1−vn−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, so W/V<q^+1 and q≤q^. Since q^0≥q^, also q^0≥q.
(B) If vn−1≥⌊β/2⌋, then q^≤q+2. If V=βn−1, then q=⌊W/βn−1⌋=wnβ+wn−1=q^0, and q≤β−1 gives q^=q. Otherwise V>βn−1. Suppose q^≥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), using V<(vn−1+1)βn−1. Also q>W/V−1. So
3≤q^−q<V−βn−1W−VW+1=VW⋅V−βn−1βn−1+1, and W/V>2(V−βn−1)/βn−1≥2(vn−1−1). Hence q≥2(vn−1−1), and β−1≥q^≥q+3≥2vn−1+1, so vn−1≤β/2−1<⌊β/2⌋, a contradiction. □
3. One step. Fix j and let W be the window uj,…,uj+n before step j, with 0≤W<βV (this is shown in part 4). Then step j sets qj=q=⌊W/V⌋ and leaves the window equal to W−qV∈[0,V).
Line L3 sets q^=q^0 and r^=wnβ+wn−1−q^vn−1, so 0≤r^<vn−1. Each round of line L4 lowers q^ by 1 and raises r^ by vn−1, so the relation q^vn−1+r^=wnβ+wn−1 is kept.
Line L4 keeps q^≥q. A round runs only if q^≥β, and then q^−1≥β−1≥q; or if q^vn−2>r^β+wn−2. In the second case, adding q^vn−1β=(wnβ+wn−1−r^)β to both sides gives q^(vn−1β+vn−2)>wnβ2+wn−1β+wn−2, so q^(vn−1β+vn−2)≥wnβ2+wn−1β+wn−2+1, and
q^V≥q^(vn−1β+vn−2)βn−2≥(wnβ2+wn−1β+wn−2+1)βn−2>W. So q<q^ and q^−1≥q. With (A) at the start, q^≥q holds throughout.
Line L4 terminates. q^ decreases by 1 in each round and stays ≥q≥0, so there are at most q^0−q rounds. With the normalization, q^0≤(vn−1β+β−1)/vn−1<β+2 (using wn≤vn−1, which follows from W<βV<(vn−1+1)βn), and (B) gives q≥min(q^0,β−1)−2, so the number of rounds is at most 4.
After line L4, q^≤q+1 and q^<β. When the loop stops, either r^≥β or (q^<β and q^vn−2≤r^β+wn−2). In the first case, q^<β as well: if q^≥β, then r^=wnβ+wn−1−q^vn−1≤vn−1β+β−1−βvn−1<β. And then q^vn−2≤(β−1)2<β2≤r^β+wn−2. So in both cases q^<β and q^vn−2≤r^β+wn−2, which as above gives q^(vn−1β+vn−2)≤wnβ2+wn−1β+wn−2. Write V=(vn−1β+vn−2)βn−2+V0 with 0≤V0<β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, so W−(q^−1)V>0, which gives q≥q^−1.
Lines L5 to L7. Line L5 computes W−q^V in n+1 digits with a borrow. If q^=q, then W−q^V∈[0,V) is not negative, line L6 does nothing, and qj=q. If q^=q+1, then W−q^V=(W−qV)−V∈[−V,0) is negative; the digits hold W−q^V+βn+1 with a borrow out, and line L6 adds V and drops the carry, which leaves W−qV, and sets qj=q^−1=q. In both cases the window ends as W−qV∈[0,V), and qj=q∈[0,β−1] is a digit.
4. The loop invariant and the end. Let U(j) be the number held in u0,…,um+n before step j, for j=m,m−1,…,0, and U(−1) the number after step 0. The invariant before step j is
U=(i>j∑qiβi)V+U(j),0≤U(j)<βj+1V. Before step m, U(m)=U and no quotient digit is set. The unnormalized dividend is below βm+n, so U<dβm+n, and the unnormalized divisor is at least βn−1, so βm+1V≥βm+1dβn−1=dβm+n. Hence U(m)<βm+1V. The digits of U(j) below position j are not touched by step j, and U(j)<βj+1V means that the window W=⌊U(j)/βj⌋ satisfies W<βV. Step j replaces W by W−qjV with qj=⌊W/V⌋ (part 3), so U(j−1)=U(j)−qjβjV and 0≤U(j−1)<βjV, since U(j−1)=(W−qjV)βj+(U(j)modβj)≤(V−1)βj+βj−1<βjV. This is the invariant for j−1.
After step 0, U=(∑i≥0qiβi)V+U(−1) with 0≤U(−1)<V, so ∑iqiβi=⌊U/V⌋=Q and U(−1) is the normalized remainder dR. Since U(−1)<V<βn, it lies in u0,…,un−1. It is divisible by d, so the short division of line L8 is exact and returns R. The loop of line L2 has m+1 steps and each step terminates (part 3), so the algorithm terminates. ■