Divisibility
Number theory studies the integers, and its starting point is divisibility. Once a pure subject, it now protects every HTTPS connection: RSA and elliptic-curve cryptography, hash functions and checksums are built on the facts in this part of the course. If primes and remainders are new to you, the school-level lessons Factors and Multiples and Divisibility Rules are a gentle start.
Definition and basic facts
For integers and , divides , written , if for some integer . Then is a divisor (factor) of and is a multiple of . For example , , , and every integer divides 0.
Facts that follow straight from the definition (for integers ):
| Fact | Reason |
|---|---|
| if and then | |
| if and then for all | |
| if and then | with |
The second fact, "a divisor of two numbers divides every integer linear combination of them", is the engine of the next lesson.
The division theorem
Theorem. For every integer and every integer there are unique integers (quotient) and (remainder) with
We write and , or . Existence: take to be the smallest non-negative number of the form (the Well Ordering Principle); if then would be smaller. Uniqueness: if with both remainders in , then with , so .
Python's divmod(n, d) returns exactly this pair for , including for negative . C, Java and JavaScript truncate toward zero instead, giving a negative remainder for negative :
import math
for n, d in [(17, 5), (-17, 5), (100, 7)]:
q, r = divmod(n, d)
print(f"divmod({n}, {d}) = ({q}, {r}) check: {q}*{d}+{r} = {q * d + r}")
print("C-style truncation for -17 / 5:", int(-17 / 5), "remainder", int(math.fmod(-17, 5)))divmod(17, 5) = (3, 2) check: 3*5+2 = 17
divmod(-17, 5) = (-4, 3) check: -4*5+3 = -17
divmod(100, 7) = (14, 2) check: 14*7+2 = 100
C-style truncation for -17 / 5: -3 remainder -2
Digit-sum tests
Why does "3 divides a number exactly when it divides its digit sum" work? A number with digits is , and is a multiple of 9 (hence of 3). So
is a multiple of 9, and and its digit sum leave the same remainder modulo 9 and modulo 3. The rule for 11 uses the alternating digit sum, because is a multiple of 11.
def digit_sum(n):
return sum(int(c) for c in str(n))
def alt_digit_sum(n):
return sum((-1) ** i * int(c) for i, c in enumerate(reversed(str(n))))
for n in [123456789, 918273645, 121, 9174, 31415926]:
print(f"{n}: n mod 9 = {n % 9}, digit sum mod 9 = {digit_sum(n) % 9};"
f" n mod 11 = {n % 11}, alt sum mod 11 = {alt_digit_sum(n) % 11}")123456789: n mod 9 = 0, digit sum mod 9 = 0; n mod 11 = 5, alt sum mod 11 = 5
918273645: n mod 9 = 0, digit sum mod 9 = 0; n mod 11 = 3, alt sum mod 11 = 3
121: n mod 9 = 4, digit sum mod 9 = 4; n mod 11 = 0, alt sum mod 11 = 0
9174: n mod 9 = 3, digit sum mod 9 = 3; n mod 11 = 0, alt sum mod 11 = 0
31415926: n mod 9 = 4, digit sum mod 9 = 4; n mod 11 = 3, alt sum mod 11 = 3
The die-hard water jug puzzle
You have a 3-gallon jug and a 5-gallon jug and an unlimited water supply. Can you measure exactly 4 gallons? With 21- and 26-gallon jugs, can you measure 3? With 6- and 9-gallon jugs, can you measure 4?
The invariant: every amount in either jug is always a linear combination (a fact about state machines). So any common divisor of the two jug sizes divides every amount you can measure. With 6 and 9, every amount is a multiple of 3, so 4 is impossible. The next lesson shows the converse: every multiple of up to is reachable.
In software
The remainder operator is everywhere: i % n wraps indices in circular buffers, hash(key) % size picks a hash-table bucket, x % 2 == 0 tests parity, and check digits (ISBN, credit cards with Luhn's algorithm, IBAN mod 97) catch typos. Be careful with negative numbers when porting code between Python and C-family languages.
Exercises
Exercise 1: True or false
(a) ; (b) ; (c) ; (d) ; (e) if and then .
Answer
(a) T, (b) T, (c) F, (d) T, (e) T.
Solution
(a) . (b) . (c) has no solution. (d) . (e) a divisor of and divides every linear combination.
Exercise 2: Quotient and remainder
Find and with for (a) , ; (b) , .
Answer
(a) , ; (b) , .
Solution
(a) . (b) ; note would give , which is not allowed.
Exercise 3: Divisible by 11
Is 8,291,072 divisible by 11? Use the alternating digit sum and check in Python.
Answer
No, the remainder is 9.
Solution
From the right: . The alternating sum is not a multiple of 11, so neither is the number; both leave remainder 9.
n = 8291072
s = sum((-1) ** i * int(c) for i, c in enumerate(reversed(str(n))))
print("alternating sum:", s, " mod 11:", s % 11, " n mod 11:", n % 11)alternating sum: 9 mod 11: 9 n mod 11: 9
Test yourself
What does divmod(-7, 2) return in Python?
- (-3, -1)
- (-4, 1)
- (-3, 1)
- (-4, -1)
If and , which number must also divide?
With jugs of 6 and 10 gallons, which amount can never be measured?
- 2
- 4
- 6
- 5