Find a course

Module 5: Number Theory and GraphsLesson 10 of 1215 min

Divisibility, GCD and Modular Arithmetic

Use divisibility and prime factorization, compute greatest common divisors with the Euclidean algorithm, and calculate with congruences and remainders.

Step 1 of 12Learn

Divisibility and primes

Every time you buy something online, your browser and the store's server agree on secret keys using arithmetic with remainders on numbers hundreds of digits long. That cryptography rests on a few ideas about whole numbers that you can learn in one lesson.

Divisibility. For integers a and b with a ≠ 0, a divides b (written a | b) if b = ak for some integer k. So 3 | 12, but 5 ∤ 12. Divisibility has handy properties: if a | b and a | c, then a divides b + c and any combination bx + cy.

Division with remainder. For any integer a and positive integer n, there are unique integers q (quotient) and r (remainder) with

a = qn + r and 0 ≤ r < n.

We write r = a mod n. For example, 47 = 6·7 + 5, so 47 mod 7 = 5. For negative numbers the remainder is still between 0 and n − 1: −5 = (−1)·7 + 2, so −5 mod 7 = 2. (Some programming languages' % operator returns −5 here instead, a classic source of bugs.)

Primes. An integer p > 1 is prime if its only positive divisors are 1 and p; otherwise it's composite. The Fundamental Theorem of Arithmetic says every integer greater than 1 factors into primes in exactly one way, apart from order: 360 = 2³·3²·5.

To test whether n is prime, you only need to try divisors up to √n: if n = ab with both factors bigger than √n, then ab > n. To test 221, try primes up to 14: 221 = 13·17.

Step 2 of 12Practice · Work it out

What is 100 mod 7?

Step 3 of 12Learn

The Euclidean algorithm

The greatest common divisor gcd(a, b) is the largest positive integer dividing both a and b. You could find it by factoring: 84 = 2²·3·7 and 36 = 2²·3², so gcd = 2²·3 = 12. But factoring huge numbers is extremely slow, and that difficulty is exactly what keeps RSA encryption secure.

Euclid found a better way about 2,300 years ago. The key fact: gcd(a, b) = gcd(b, a mod b). Any number dividing a and b also divides a − qb, the remainder, and vice versa. So keep replacing the pair with (smaller number, remainder) until the remainder is 0. The last nonzero remainder is the gcd.

Example. gcd(252, 198):

  • 252 = 1·198 + 54
  • 198 = 3·54 + 36
  • 54 = 1·36 + 18
  • 36 = 2·18 + 0

So gcd(252, 198) = 18.

The algorithm is fast: the numbers shrink at least by half every two steps, so even 600-digit numbers need only a few thousand steps.

Bézout's identity. Running the steps backward writes the gcd as a combination of the original numbers: there are integers x and y with ax + by = gcd(a, b). From the steps above, 18 = 54 − 36 = 54 − (198 − 3·54) = 4·54 − 198 = 4(252 − 198) − 198 = 4·252 − 5·198. Check: 1,008 − 990 = 18.

Two numbers are relatively prime if their gcd is 1, like 8 and 15. Bézout's identity then says 8x + 15y = 1 has integer solutions, which is the key to dividing in modular arithmetic.

Step 4 of 12Diagram

Euclid on gcd(84, 36)

Euclid on gcd(84, 36)Process diagram: 84 = 2 · 36 + 12 → 36 = 3 · 12 + 0 → gcd = 12.84 = 2 · 36 + 1236 = 3 · 12 + 0gcd = 12

Tap a shape with to see more.

84 = 2 · 36 + 12

gcd(84, 36) = gcd(36, 12).

36 = 3 · 12 + 0

Remainder 0: stop.

gcd = 12

The last non-zero remainder. Two steps instead of factoring.

Step 5 of 12Practice · Put in order

Order the steps of the Euclidean algorithm for gcd(105, 30).

Drag the items, or use the arrow buttons, to put them in order.

  1. 30 = 2·15 + 0
  2. The remainder is 0, so stop
  3. 105 = 3·30 + 15
  4. The last nonzero remainder, 15, is the gcd

Step 6 of 12Practice · Work it out

Use the Euclidean algorithm to find gcd(391, 299).

Step 7 of 12Learn

Modular arithmetic

Clock arithmetic. If it's 9 o'clock now, in 5 hours it will be 2 o'clock, because 9 + 5 = 14 and 14 mod 12 = 2. Modular arithmetic is arithmetic where numbers wrap around.

We write a ≡ b (mod n), "a is congruent to b modulo n," when n divides a − b, or equivalently when a and b leave the same remainder. So 14 ≡ 2 (mod 12) and 38 ≡ 3 (mod 7). This is the equivalence relation from Module 3; its classes are the remainders 0, 1, …, n − 1.

The key property is that congruences respect addition and multiplication: if a ≡ b and c ≡ d (mod n), then a + c ≡ b + d and ac ≡ bd (mod n). So you can reduce at every step and keep numbers small.

Example. What's the last digit of 7¹⁰⁰? That's 7¹⁰⁰ mod 10. Powers of 7 end in 7, 9, 3, 1, 7, 9, 3, 1, … repeating every 4 (because 7⁴ = 2401 ≡ 1). Since 100 is a multiple of 4, 7¹⁰⁰ ≡ (7⁴)²⁵ ≡ 1²⁵ = 1. The last digit is 1.

Division is the tricky operation. You can't always divide in modular arithmetic. A number a has a multiplicative inverse mod n (a number x with ax ≡ 1) exactly when gcd(a, n) = 1. The inverse of 3 mod 7 is 5, since 15 ≡ 1 (mod 7). But 2 has no inverse mod 4.

Applications. Check digits on ISBNs and credit card numbers use mod 10 or mod 11 sums to catch typos. Hash tables place a key in bucket (hash mod table size). And RSA encryption raises a message to a large power mod n = pq, where p and q are secret primes; decryption uses an inverse exponent that only someone who knows p and q can compute.

Step 8 of 12Practice · Diagram

Days of the week are arithmetic mod 7

Today is Monday. Which day is it in 10 days?

Tap the shape that answers the question, then press Check.

Days of the week are arithmetic mod 7Cycle diagram: Mon (0) → Tue (1) → Wed (2) → Thu (3) → Fri (4) → Sat (5) → Sun (6).mod 7Mon (0)Tue (1)Wed (2)Thu (3)Fri (4)Sat (5)Sun (6)
Mon (0)
Tue (1)
Wed (2)
Thu (3)
Fri (4)
Sat (5)
Sun (6)

Step 9 of 12Try the calculator

Modular arithmetic calculator

Enter integers a and b and a modulus n. The calculator reduces them and shows that reducing first gives the same answer as reducing at the end. Try a negative a to see how mathematical mod differs from some programming languages.

a mod n
5
b mod n
3
(a + b) mod n
1
(a·b) mod n
1

Step 10 of 12Practice · Fill in the blanks

Fill in the blanks

Today is Wednesday. In 100 days it will be , because 100 mod 7 = .

Step 11 of 12Note

Step 12 of 12Practice · Choose one

What is the multiplicative inverse of 4 modulo 9?