Study guide: Number Theory
Overview
Number theory studies the integers: divisibility, primes, greatest common divisors and congruences. It looks old-fashioned, yet it powers RSA encryption, hash tables, checksums and pseudo-random generators.
Almost everything rests on the division algorithm: for integers a and b > 0 there are unique q and r with a = bq + r and 0 ≤ r < b. The Euclidean algorithm is just that statement applied repeatedly.
Key ideas explained
Divisibility and primes
a divides b when b = ak for some integer k. A prime has exactly two positive divisors. Every integer greater than 1 factors into primes in exactly one way (the Fundamental Theorem of Arithmetic), and to test whether n is prime you only need to try divisors up to √n.
GCD, LCM and the Euclidean algorithm
gcd(a, b) = gcd(b, a mod b); stop when the remainder is 0. The number of steps grows only logarithmically. Also lcm(a, b) = ab / gcd(a, b). Bézout's identity says gcd(a, b) = ax + by for some integers x and y, and the extended Euclidean algorithm finds them.
Modular arithmetic
a ≡ b (mod m) means m divides a − b. You can add, subtract and multiply congruences freely. Division is only possible when the divisor has an inverse: a has an inverse modulo m exactly when gcd(a, m) = 1, and the extended Euclidean algorithm computes it.
CRT, Fermat and Euler
Chinese Remainder Theorem: if the moduli are pairwise coprime, the system x ≡ aᵢ (mod mᵢ) has a unique solution modulo their product. Fermat: if p is prime and p does not divide a, then a^(p−1) ≡ 1 (mod p). Euler generalises this: if gcd(a, n) = 1 then a^φ(n) ≡ 1 (mod n), where φ(n) counts the integers from 1 to n that are coprime to n.
Worked examples
GCD, LCM and a Bézout combination
Problem. Find gcd(252, 198), lcm(252, 198), and integers x, y with 252x + 198y = gcd.
- 252 = 1·198 + 54
- 198 = 3·54 + 36
- 54 = 1·36 + 18
- 36 = 2·18 + 0, so the gcd is 18.
- lcm = 252·198 / 18 = 2772.
- Back-substitute: 18 = 54 − 36 = 54 − (198 − 3·54) = 4·54 − 198 = 4(252 − 198) − 198 = 4·252 − 5·198.
- Check: 1008 − 990 = 18.
Answer. gcd = 18, lcm = 2772, and 18 = 4·252 + (−5)·198.
A Chinese Remainder system
Problem. Solve x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7).
- From the first two: 8 satisfies both (8 mod 3 = 2, 8 mod 5 = 3), so x ≡ 8 (mod 15).
- Write x = 8 + 15t. Modulo 7 this is 1 + t, and we need 2, so t ≡ 1.
- Then x = 23, unique modulo 3·5·7 = 105.
Answer. x ≡ 23 (mod 105).
Enter these same values in the calculator above and compare each step with the working shown here.
Common mistakes to avoid
- Treating 1 as prime. Excluding it is what makes prime factorization unique.
- Cancelling a factor from both sides of a congruence when it shares a factor with the modulus: 2·3 ≡ 2·0 (mod 6), yet 3 ≢ 0 (mod 6).
- Using Fermat's little theorem when the modulus is composite or when p divides a.
- Mixing up gcd and lcm. The gcd never exceeds the smaller number, and gcd × lcm = ab.
Where this shows up in computer science
- RSA: encryption is modular exponentiation, and the private exponent is a modular inverse modulo φ(n).
- Check digits (ISBN, credit-card Luhn check) and hash-table indexing use modular arithmetic.
- Finding when two repeating events coincide is a least-common-multiple problem.
Test yourself
Try each question before opening the answer.
Find the inverse of 7 modulo 40.
23, because 7·23 = 161 = 4·40 + 1.
Compute 3¹⁰⁰ mod 7.
By Fermat 3⁶ ≡ 1, and 100 = 6·16 + 4, so 3¹⁰⁰ ≡ 3⁴ = 81 ≡ 4 (mod 7).
What is φ(36)?
36·(1 − 1/2)·(1 − 1/3) = 12.