TExcelTAD
Home › Number Theory & Cryptography › The Division Algorithm

The Division Algorithm

Long division from school is a theorem: every integer can be divided by a positive integer with a unique quotient and a remainder smaller than the divisor. It is the first tool in every number-theory algorithm and the reason the remainder operator % exists.

The theorem

For every integer aa and every integer n>0n > 0 there are unique integers qq (the quotient) and rr (the remainder) with

a=qn+r,0≤r<na = qn + r, \qquad 0 \le r < n

Existence. Take q=⌊a/n⌋q = \lfloor a/n \rfloor, the largest integer with qn≤aqn \le a. Then r=a−qn≥0r = a - qn \ge 0, and r<nr < n, since otherwise (q+1)n≤a(q + 1)n \le a would contradict the choice of qq.

Uniqueness. If a=qn+r=q′n+r′a = qn + r = q'n + r' with both remainders in [0,n)[0, n), then n(q−q′)=r′−rn(q - q') = r' - r. The right side lies strictly between −n-n and nn and is a multiple of nn, so it is 0. Hence r=r′r = r' and q=q′q = q'.

Notice that n∣an \mid a exactly when r=0r = 0.

python · try it yourself
for a, n in [(100, 7), (-100, 7), (7, 100), (0, 3)]:
    q, r = divmod(a, n)
    assert a == q * n + r and 0 <= r < n
    print(f"a={a:5}  n={n:3}  q={q:4}  r={r}")
a=  100  n=  7  q=  14  r=2
a= -100  n=  7  q= -15  r=5
a=    7  n=100  q=   0  r=7
a=    0  n=  3  q=   0  r=0

Writing numbers in a base

Repeated division gives the digits of a number in any base b≥2b \ge 2: the remainder of aa divided by bb is the last digit, and the quotient holds the remaining digits. This is how bin, hex and every toString(radix) work.

python · try it yourself
def to_base(a, b):
    digits = "0123456789abcdefghijklmnopqrstuvwxyz"
    out = ""
    while True:
        a, r = divmod(a, b)
        out = digits[r] + out
        if a == 0:
            return out

print(to_base(2026, 2), bin(2026))
print(to_base(2026, 16), hex(2026))
print(to_base(2026, 36), int(to_base(2026, 36), 36))
11111101010 0b11111101010
7ea 0x7ea
1ka 2026

Reading it the other way, Horner's rule rebuilds the number: 2026=((7⋅16+14)⋅16+10)2026 = ((7 \cdot 16 + 14) \cdot 16 + 10), so the hex digits are 7ea.

Remainders for powers of two

When n=2kn = 2^k, the quotient is a right shift and the remainder is the low kk bits: a=(a≫k)⋅2k+(a&(2k−1))a = (a \gg k) \cdot 2^k + (a \mathbin{\&} (2^k - 1)). CPUs and compilers use this to avoid slow division instructions.

python · try it yourself
a, k = 123456789, 10
print(divmod(a, 2**k), (a >> k, a & (2**k - 1)))
(120563, 277) (120563, 277)

In software

  • divmod and % with a negative left side differ between languages: Python and Ruby floor the quotient (remainder has the sign of nn), C, Java, Go, Rust and JavaScript truncate it (remainder has the sign of aa). Use ((a % n) + n) % n in C-like languages to get the mathematical remainder.
  • Big-integer libraries implement the division algorithm digit by digit (Knuth's Algorithm D) on 32- or 64-bit "limbs".

Exercises

Exercise 1: By hand

Find qq and rr for a=250a = 250, n=17n = 17 and for a=−250a = -250, n=17n = 17.

Answer

250=14⋅17+12250 = 14 \cdot 17 + 12; −250=−15⋅17+5-250 = -15 \cdot 17 + 5.

Solution

14⋅17=23814 \cdot 17 = 238 and 250−238=12250 - 238 = 12. For −250-250, the largest multiple of 17 not above −250-250 is −15⋅17=−255-15 \cdot 17 = -255, and −250−(−255)=5-250 - (-255) = 5. Note that the two remainders add up to 17: negating aa turns a remainder r≠0r \ne 0 into n−rn - r.

Exercise 2: Remainders of squares

Show that every perfect square leaves remainder 0 or 1 when divided by 4. Conclude that no number of the form 4k+34k + 3 is a sum of two squares.

Answer

(2m)2=4m2(2m)^2 = 4m^2 and (2m+1)2=4(m2+m)+1(2m + 1)^2 = 4(m^2 + m) + 1.

Solution

By the division algorithm every integer is 2m2m or 2m+12m + 1. Squaring gives remainder 0 or 1 modulo 4. A sum of two squares therefore has remainder 0+00 + 0, 0+10 + 1 or 1+11 + 1, that is 0, 1 or 2, never 3.

Exercise 3: Base 7 (code)

Write 2026 in base 7 using to_base and check it with int(s, 7).

Answer

5623.

Solution

5⋅343+6⋅49+2⋅7+3=1715+294+14+3=20265 \cdot 343 + 6 \cdot 49 + 2 \cdot 7 + 3 = 1715 + 294 + 14 + 3 = 2026.

python · try it yourself
def to_base(a, b):
    out = ""
    while True:
        a, r = divmod(a, b)
        out = str(r) + out
        if a == 0:
            return out
s = to_base(2026, 7)
print(s, int(s, 7))
5623 2026

Test yourself

In the division algorithm a=qn+ra = qn + r, the remainder must satisfy…

  1. 0≤r<n0 \le r < n
  2. −n<r<n-n < r < n
  3. 0<r≤n0 < r \le n
  4. rr has the sign of aa

What is the last hexadecimal digit of a number aa?

  1. a mod 10a \bmod 10
  2. a mod 16a \bmod 16
  3. ⌊a/16⌋\lfloor a / 16 \rfloor
  4. a mod 8a \bmod 8

For n=2kn = 2^k, the remainder of aa divided by nn equals…

  1. a >> k
  2. a & (n - 1)
  3. a | n
  4. a ^ n