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 and every integer there are unique integers (the quotient) and (the remainder) with
Existence. Take , the largest integer with . Then , and , since otherwise would contradict the choice of .
Uniqueness. If with both remainders in , then . The right side lies strictly between and and is a multiple of , so it is 0. Hence and .
Notice that exactly when .
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 : the remainder of divided by is the last digit, and the quotient holds the remaining digits. This is how bin, hex and every toString(radix) work.
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: , so the hex digits are 7ea.
Remainders for powers of two
When , the quotient is a right shift and the remainder is the low bits: . CPUs and compilers use this to avoid slow division instructions.
a, k = 123456789, 10
print(divmod(a, 2**k), (a >> k, a & (2**k - 1)))(120563, 277) (120563, 277)
In software
divmodand%with a negative left side differ between languages: Python and Ruby floor the quotient (remainder has the sign of ), C, Java, Go, Rust and JavaScript truncate it (remainder has the sign of ). Use((a % n) + n) % nin 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 and for , and for , .
Answer
; .
Solution
and . For , the largest multiple of 17 not above is , and . Note that the two remainders add up to 17: negating turns a remainder into .
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 is a sum of two squares.
Answer
and .
Solution
By the division algorithm every integer is or . Squaring gives remainder 0 or 1 modulo 4. A sum of two squares therefore has remainder , or , 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
.
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 , the remainder must satisfy…
- has the sign of
What is the last hexadecimal digit of a number ?
For , the remainder of divided by equals…
a >> ka & (n - 1)a | na ^ n