Divisibility
Number theory starts with one relation between integers: one number divides another. Everything else in this course, from the gcd to RSA, is built from it.
Definition
For integers and , we say divides , written , if there is an integer with . Then is a divisor (or factor) of , and is a multiple of . For example because , while .
Some edge cases follow straight from the definition: for every (take ), and for every , and only when .
In code, for is the test b % a == 0.
def divides(a, b):
return b == 0 if a == 0 else b % a == 0
print(divides(6, 42), divides(6, 45), divides(7, 0), divides(0, 5))
print("divisors of 60:", [d for d in range(1, 61) if 60 % d == 0])True False True False
divisors of 60: [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60]
Basic properties
For integers :
- Transitivity: if and , then .
- Linear combinations: if and , then .
- Size: if and , then .
Property 2 is the workhorse. Its proof is one line: write and ; then . It is why Euclid's algorithm works: any common divisor of and also divides .
Divisibility tests are modular arithmetic
The school rule "a number is divisible by 9 when its digit sum is" is property 2 in disguise. Because , every power leaves remainder 1 when divided by 9, so a number and its digit sum have the same remainder. The same idea gives the alternating-sum test for 11, since modulo 11 (the language of Congruences).
n = 918273645
digits = [int(c) for c in str(n)]
alt = sum(d if i % 2 == 0 else -d for i, d in enumerate(reversed(digits)))
print("digit sum:", sum(digits), " n % 9 =", n % 9)
print("alternating sum:", alt, " n % 11 =", n % 11)digit sum: 45 n % 9 = 0
alternating sum: 25 n % 11 = 3
The digit sum is 45, a multiple of 9, so . The alternating sum and leave the same remainder modulo 11.
Counting divisors
If you list divisors in pairs and , you only need to search up to . The number of divisors and their sum have neat formulas once we know unique factorization.
from math import isqrt
def divisors(n):
small = [d for d in range(1, isqrt(n) + 1) if n % d == 0]
large = [n // d for d in reversed(small) if d * d != n]
return small + large
print(divisors(360))
print("tau(360) =", len(divisors(360)), " sigma(360) =", sum(divisors(360)))
print("perfect numbers below 10000:", [n for n in range(2, 10000) if sum(divisors(n)) == 2 * n])[1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 18, 20, 24, 30, 36, 40, 45, 60, 72, 90, 120, 180, 360]
tau(360) = 24 sigma(360) = 1170
perfect numbers below 10000: [6, 28, 496, 8128]
A perfect number equals the sum of its proper divisors, so : 6, 28, 496 and 8128 are the only ones below 10000.
In software
- Checking
x % n == 0is how code tests for multiples; when is a power of two, compilers turn it into a bit maskx & (n - 1) == 0, which is why buffer sizes and memory alignments are powers of two. - Hash tables often use a prime number of buckets so that keys sharing a common factor with the table size do not pile up in a few buckets.
Exercises
Exercise 1: True or false
Decide: (a) ; (b) ; (c) ; (d) if then or .
Answer
(a) true, (b) false, (c) true, (d) false in general.
Solution
(a) . (b) is not an integer. (c) . (d) but and . The statement is true when is prime: that is Euclid's lemma (Unique Factorization).
Exercise 2: A linear combination
Show that if and , then , so for every integer .
Answer
.
Solution
By the linear-combination property, divides . The only positive divisor of is 1, so the two numbers have no common factor larger than 1, whatever is.
Exercise 3: Highly divisible (code)
Which number below 1000 has the most divisors?
Answer
840, with 32 divisors.
Solution
best = max(range(1, 1000), key=lambda n: sum(1 for d in range(1, n + 1) if n % d == 0))
print(best, sum(1 for d in range(1, best + 1) if best % d == 0))840 32
has divisors.
Test yourself
Which statement is true for every integer ?
If and , which number must also divide?
Why does the digit-sum test work for 9?
- 9 is prime
- every power of 10 leaves remainder 1 when divided by 9
- 9 is odd
- digit sums are always multiples of 9