TExcelTAD
Home › Number Theory & Cryptography › Divisibility

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 aa and bb, we say aa divides bb, written a∣ba \mid b, if there is an integer kk with b=kab = ka. Then aa is a divisor (or factor) of bb, and bb is a multiple of aa. For example 6∣426 \mid 42 because 42=7⋅642 = 7 \cdot 6, while 6∤456 \nmid 45.

Some edge cases follow straight from the definition: a∣0a \mid 0 for every aa (take k=0k = 0), 1∣b1 \mid b and −1∣b-1 \mid b for every bb, and 0∣b0 \mid b only when b=0b = 0.

In code, a∣ba \mid b for a≠0a \ne 0 is the test b % a == 0.

python · try it yourself
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 a,b,c,x,ya, b, c, x, y:

  1. Transitivity: if a∣ba \mid b and b∣cb \mid c, then a∣ca \mid c.
  2. Linear combinations: if a∣ba \mid b and a∣ca \mid c, then a∣bx+cya \mid bx + cy.
  3. Size: if a∣ba \mid b and b≠0b \ne 0, then ∣a∣≤∣b∣\lvert a \rvert \le \lvert b \rvert.

Property 2 is the workhorse. Its proof is one line: write b=kab = ka and c=lac = la; then bx+cy=(kx+ly)abx + cy = (kx + ly)a. It is why Euclid's algorithm works: any common divisor of aa and bb also divides a−qba - qb.

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 10=9+110 = 9 + 1, every power 10k10^k 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 10≡−110 \equiv -1 modulo 11 (the language of Congruences).

python · try it yourself
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 9∣9182736459 \mid 918273645. The alternating sum and nn leave the same remainder modulo 11.

Counting divisors

If you list divisors in pairs dd and n/dn/d, you only need to search up to n\sqrt{n}. The number of divisors τ(n)\tau(n) and their sum σ(n)\sigma(n) have neat formulas once we know unique factorization.

python · try it yourself
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 σ(n)=2n\sigma(n) = 2n: 6, 28, 496 and 8128 are the only ones below 10000.

In software

  • Checking x % n == 0 is how code tests for multiples; when nn is a power of two, compilers turn it into a bit mask x & (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) 7∣917 \mid 91; (b) −4∣18-4 \mid 18; (c) 13∣013 \mid 0; (d) if a∣bca \mid bc then a∣ba \mid b or a∣ca \mid c.

Answer

(a) true, (b) false, (c) true, (d) false in general.

Solution

(a) 91=13⋅791 = 13 \cdot 7. (b) 18/4=4.518 / 4 = 4.5 is not an integer. (c) 0=0⋅130 = 0 \cdot 13. (d) 6∣4⋅9=366 \mid 4 \cdot 9 = 36 but 6∤46 \nmid 4 and 6∤96 \nmid 9. The statement is true when aa is prime: that is Euclid's lemma (Unique Factorization).

Exercise 2: A linear combination

Show that if d∣5n+3d \mid 5n + 3 and d∣3n+2d \mid 3n + 2, then d∣1d \mid 1, so gcd⁡(5n+3,3n+2)=1\gcd(5n + 3, 3n + 2) = 1 for every integer nn.

Answer

3(5n+3)−5(3n+2)=−13(5n + 3) - 5(3n + 2) = -1.

Solution

By the linear-combination property, dd divides 3(5n+3)−5(3n+2)=15n+9−15n−10=−13(5n + 3) - 5(3n + 2) = 15n + 9 - 15n - 10 = -1. The only positive divisor of −1-1 is 1, so the two numbers have no common factor larger than 1, whatever nn is.

Exercise 3: Highly divisible (code)

Which number below 1000 has the most divisors?

Answer

840, with 32 divisors.

Solution
python · try it yourself
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

840=23⋅3⋅5⋅7840 = 2^3 \cdot 3 \cdot 5 \cdot 7 has (3+1)(1+1)(1+1)(1+1)=32(3 + 1)(1 + 1)(1 + 1)(1 + 1) = 32 divisors.

Test yourself

Which statement is true for every integer aa?

  1. 0∣a0 \mid a
  2. a∣0a \mid 0
  3. a∣1a \mid 1
  4. 2∣a2 \mid a

If a∣ba \mid b and a∣ca \mid c, which number must aa also divide?

  1. b/cb / c
  2. 3b−7c3b - 7c
  3. b2+1b^2 + 1
  4. b+1b + 1

Why does the digit-sum test work for 9?

  1. 9 is prime
  2. every power of 10 leaves remainder 1 when divided by 9
  3. 9 is odd
  4. digit sums are always multiples of 9