We consider the task of dividing a positive integer u by another positive integer v, thus obtaining a quotient q=⌊u/v⌋ and a remainder r such that u=qv+r with 0≤r<v.
The method presented here is based on The Classical Algorithms, Section 4.3.1, of The Art of Computer Programming, Volume 2, by Donald E. Knuth. The material is quite theory-heavy and if you are just looking for the main algorithm, you can skip to the bottom and Algorithm L.
We represent the numbers using radix b≥2 and set
u=(um−1…u1u0)bandv=(vn−1…v1v0)b,
so u is an m-digit number and v is an n-digit number (see previous post for more details on representing multiple-precision numbers).
Two special cases are easily dealt with:
- If m<n then u<v and so q=0 and r=u is the simple answer.
- If n=1 then v is just a single digit and we use a short division algorithm instead.
So in the following we assume that m≥n>1.
We will approach the division algorithm from a top-level point of view. It is actually just a formalization of the well-known pencil-and-paper method:
Algorithm G. Given u=(um−1…u1u0)b, um−1=0 and v=(vn−1…v1v0)b, vn−1=0, with m≥n>0, this algorithm outlines how to compute the quotient q=(qm−n…q1q0)b=⌊u/v⌋ (we may have qm−n=0 in which case qm−n−1=0 if m>n) and the remainder r such that u=qv+r, 0≤r<v.
- G1. u(m−n+1)←(0um−1…u1u0)b.
- G2. k←m−n.
- G3. qk←⌊(uk+n(k+1)…uk+1(k+1)uk(k+1))b/v⌋.
- G4. Set u(k)←u(k+1)−qkbkv or, equivalently,
(uk+n(k)…uk+1(k)uk(k))b(uk−1(k)…u1(k)u0(k))b←(uk+n(k+1)…uk+1(k+1)uk(k+1))b−qkv,←(uk−1(k+1)…u1(k+1)u0(k+1))b.
- G5. If k=0 then set r←u(0) and exit. Otherwise set k←k−1 and go to step G3.
An essential invariant of this algorithm is
(uk+n−1(k)…uk+1(k)uk(k))b<vfork=0,1,…,m−n+1.
This can be seen as follows. For k=m−n+1 the invariant is ensured by introducing a zero as the most significant digit of u(m−n+1) in step G1. For k=0,1,…,m−n we see from steps G3 and G4 that (uk+n(k)…uk+1(k)uk(k))b=(uk+n(k+1)…uk+1(k+1)uk(k+1))b mod v and the inequality follows.
Note that the invariant implies that uk+n(k)=0 for k=0,1,…,m−n. Furthermore we have that
(uk+n(k+1)…uk+1(k+1)uk(k+1))b=(uk+n(k+1)…uk+1(k+1))b⋅b+uk(k+1)≤(v−1)b+(b−1)=vb−1
from which we see that the quotients qk computed in step G3 are non-negative and smaller than b, as they should be.
Finally, we can verify that the algorithm computes what we intended. We have
r=u(0)=u(1)−q0b0v=u(2)−q1b1v−q0b0v=…=u(m−n+1)−(qm−nbm−n+⋯+q0b0)v=u−qv.
Now for some practical aspects. Note first that all of the u(k) variables can in fact be represented by a single variable and simply overwrite its digits along the way – thus ending up with the remainder. Note also that any of the remainder's digits may be zero.
Finally, how do we compute the quotient in step G3? That is in fact the central part of the division algorithm and is the subject of the rest of this post.
Let us now consider computing the quotient in step G3 in the case n>1. We therefore assume u=(un…u1u0)b, u<bn+1, and v=(vn−1…v1v0)b, bn−1≤v<bn, with n≥2 and 0≤⌊u/v⌋<b.
We wish to compute q=⌊u/v⌋ as fast as possible. How good is a 'first order' approximation, where we use just the two most-significant digits of u and the most-significant digit of v: (unb+un−1)/vn−1? First of all, if un=vn−1 this quantity equals b and we know that q≤b−1 by assumption, so let us therefore study
q^=min(⌊vn−1unb+un−1⌋,b−1)
This approximate quotient is never too small, as the following theorem states.
Theorem 1. With q^ as defined above we have q≤q^.
Proof. If q^=b−1 then since q≤b−1 by assumption, the statement is true.
Assume then that q^=⌊(unb+un−1)/vn−1⌋. From the properties of the floor function we have unb+un−1≤q^vn−1+vn−1−1 and therefore q^vn−1≥unb+un−1−vn−1+1. We then get
u−q^v≤u−q^vn−1bn−1≤unbn+⋯+u0−(unb+un−1−vn−1+1)bn−1=un−2bn−2+⋯+u0−bn−1+vn−1bn−1<vn−1bn−1≤v.
So u−q^v<v and since 0≤u−qv<v we must have q≤q^. ◼
If u and v are scaled appropriately, q^ will never be too large, either.
Theorem 2. With q^ as defined above and vn−1≥⌊b/2⌋, we have q^≤q+2.
Proof. Assume that q^≥q+3. We get
q^≤vn−1unbun−1=vn−1bn−1unbnun−1bn−1≤vn−1bn−1u<v−bn−1u,
since v=vn−1bn−1+⋯+v0≤vn−1bn−1+bn−1. We cannot have v=bn−1 since that would imply q^=q=un. The relation q=⌊u/v⌋ implies q>u/v−1, from which we get
3≤q^−q<v−bn−1u−vu+1=vu(v−bn−1bn−1)+1.
We then have
vu≥2(bn−1v−bn−1)≥2(vn−1−1),
and finally
b−4≥q^−3≥q=⌊u/v⌋≥2(vn−1−1),
which implies vn−1<⌊b/2⌋. ◼
We would expect to come even closer if we consider the 'second order' approximate quotient,
⌊vn−1b+vn−2unb2+un−1b+un−2⌋,
but how much closer? Given some approximate quotient q^, let us compute the corresponding second order residual
unb2+un−1b+un−2−q^(vn−1b+vn−2)=r^b+un−2−q^vn−2,
where r^ is the first order residual,
r^=unb+un−1−q^vn−1.
By studying the sign of the second order residual we can now get closer to the true quotient.
Theorem 3. Let q^ be any approximate quotient and r^ the corresponding first order residual. Now if q^vn−2>br^+un−2 then q<q^.
Proof. Assume q^vn−2>br^+un−2, equivalent to r^b+un−2−q^vn−2+1≤0. We then have
u−q^v≤u−q^vn−1bn−1−q^vn−2bn−2=bn−1(unb+un−1−q^vn−1)+un−2bn−2+⋯+u0−q^vn−2bn−2<bn−1r^+un−2bn−2+bn−2−q^vn−2bn−2=bn−2(r^b+un−2−q^vn−2+1)≤0.
So u−q^v<0≤u−qv which implies q<q^. ◼
Theorem 4. Let q^ be any approximate quotient and r^ the corresponding first order residual. Now if q^vn−2≤br^+un−2 then q^≤q+1.
Proof. Let q^vn−2≤br^+un−2 and assume q^≥q+2. Now since u−qv<v we get
u<(q+1)v≤(q^−1)v<q^(vn−1bn−1+vn−2bn−2+bn−2)−v<q^vn−1bn−1+q^vn−2bn−2+bn−1−v≤q^vn−1bn−1+(br^+un−2)bn−2+bn−1−v=unbn+un−1bn−1+un−2bn−2+bn−1−v≤unbn+un−1bn−1+un−2bn−2≤u.
This claims that u<u, a contradiction, so our assumption q^≥q+2 must have been wrong. ◼
We now have the following procedure for computing q^, a very close estimate to q:
Algorithm Q. Let u=(un…u1u0)b and v=(vn−1…v1v0)b, vn−1=0, with n≥2 and 0≤⌊u/v⌋<b (any digit of u can be zero and note that the only digits accessed are un, un−1, un−2, vn−1, and vn−2). The algorithm computes an integer q^ such that q^−1≤⌊u/v⌋≤q^ (Theorems 1 and 4).
- Q1. Set q^←⌊(unb+un−1)/vn−1⌋ and r^←(unb+un−1) mod vn−1. If q^=b (division overflow when b=bT) set q^←q^−1 and r^←r^+vn−1 (dealing with division overflow can be avoided by setting q^←b−1 and r^←un+un−1 if vn−1=un).
- Q2. While r^<b and q^vn−2>br^+un−2, set q^←q^−1 and r^←r^+vn−1 (Theorem 2 assures that this while-loop is executed at most two times if vn−1≥⌊b/2⌋. The check r^<b is not necessary but makes sure that we don't deal with numbers that are b2 or larger in the subsequent comparison).
We can now combine Algorithm G with the just obtained knowledge of approximating the quotient in the following algorithm for long division:
Algorithm L. Given u=(um−1…u1u0)b, um−1=0 and v=(vn−1…v1v0)b, vn−1=0, with m≥n>1, this algorithm computes the quotient q=(qm−n…q1q0)b=⌊u/v⌋ (we may have qm−n=0 in which case qm−n−1=0 if m>n) and the remainder r such that u=qv+r, 0≤r<v.
- L1. Set v←d⋅v such that vn−1≥⌊b/2⌋ (letting d be a power of two is usually the best choice). Similarly, set (um…u1u0)b←d⋅u (ensure u gets n+1 digits, setting um=0 if necessary).
- L2. Set k←m−n.
- L3. Find q^ such that q^−1≤⌊(uk+n…uk+1uk)b/v⌋≤q^ (use Algorithm Q described above).
- L4. Make the update (uk+n…uk+1uk)b←(uk+n…uk+1uk)b−q^v.
- L5. If the subtraction of step L4 produces a borrow (the result is negative) do q^←q^−1 and (uk+n…uk+1uk)b←(uk+n…uk+1uk)b+v.
- L6. Set qk=q^.
- L7. If k=0 set r←u/d and exit. Otherwise set k←k−1 and go to step L3.
The normalization in step L1 such that vn−1≥⌊b/2⌋ does two things. Firstly, it makes sure that the while-loop of the q^-computation executes at most two times. Secondly, the probability that the adding back in step L5 must be executed is of order 2/b (a proof can be found in Knuth's book).