Let us consider short division, by which we mean a multiple-digit number u=(um−1…u1u0)b divided by a single digit v (see, e.g., post on number representation). We will assume m≥1, um−1=0 and 0<v<b.
We are interested in a quotient q=⌊u/v⌋ and a remainder r such that u=qv+r with 0≤r<v. Using that bm−1≤u<bm and 0<v<b we can deduce that bm−2<q<bm which means that q can be represented using m−1 or m digits: q=(qm−1…q1q0)b (we may have qm−1=0 in which case qm−2=0).
We now have the following straightforward algorithm:
Algorithm S. Given u=(um−1…u1u0)b and 0<v<b with m≥1 and um−1=0, this algorithm computes the quotient q=(qm−1…q1q0)b=⌊u/v⌋ (we may have qm−1=0 in which case qm−2=0 if m>1) and the remainder r0 such that u=qv+r0, 0≤r0<v.
- S1. Set rm←0, k←m−1.
- S2. Set qk←⌊(rk+1b+uk)/v⌋, rk←(rk+1b+uk) mod v.
- S3. If k=0 then exit. Otherwise set k←k−1 and go to step S2.
By the definition of integer division and modulus, the quantities in step S2 imply rk+1b+uk=qkv+rk for k=0,1,…,m−1. Using this, we can show the correctness of the algorithm:
u=k=0∑m−1ukbk=k=0∑m−1(qkv+rk−rk+1b)bk=k=0∑m−1qkbkv+k=0∑m−1rkbk−k=1∑mrkbk=qv+r0−rmbm=qv+r0
since rm=0. It is clear from the definition of rk that 0≤rk<v for k=0,1,…,m. Considering now the definition of qk we see that since rk+1b+uk≤(v−1)b+b−1=bv−1 we will have 0≤qk<b for k=0,1,…,m−1.
Note two important things: During the course of the algorithm, we only need to keep track of one r-value, not one for each k (it just made the analysis easier). Note also that each entry of u can be overwritten with the coefficients/digits of q, possibly saving some storage.