Key idea of this section
gcd(a, n) = 1 ⟹ aφ(n) ≡ 1 (mod n); (p − 1)! ≡ −1 (mod p).
Euler's theorem as group theory
The cleanest route to the classical congruences is Lagrange's theorem from group theory: in a finite group G, the order of every element divides |G|. (The cosets of the cyclic subgroup ⟨g⟩ partition G into blocks of equal size ord(g).) Applying it to G = (ℤ/nℤ)×, of order φ(n), gives at once the following.
Theorem 4.1 (Euler, 1763). If gcd(a, n) = 1 then aφ(n) ≡ 1 (mod n).
Corollary 4.2 (Fermat's little theorem, 1640). If p is prime then aᵖ ≡ a (mod p) for every integer a, and ap−1 ≡ 1 (mod p) if p ∤ a.
A proof that avoids group theory, and is essentially Euler's own: if r₁, …, rφ(n) are the residues coprime to n, then ar₁, …, arφ(n) are the same residues in a different order (multiplication by a unit is a bijection), so ∏(arᵢ) ≡ ∏ rᵢ, and cancelling the unit ∏ rᵢ gives aφ(n) ≡ 1. A third proof of Fermat's theorem uses the binomial theorem: by Lesson 2, p | C(p, k) for 0 < k < p, so (a + 1)ᵖ ≡ aᵖ + 1 (mod p), and induction on a gives aᵖ ≡ a. The identity (x + y)ᵖ = xᵖ + yᵖ in characteristic p says that the Frobenius map x ↦ xᵖ is a ring homomorphism, a fact of enormous importance in algebraic geometry over finite fields.
Fast exponentiation
Euler's theorem reduces exponents modulo φ(n). Combined with repeated squaring, which computes aᵏ mod n with O(log k) multiplications, it makes huge powers easy. Worked example: 3¹⁰⁰ mod 7. Since 3⁶ ≡ 1 (mod 7) and 100 = 6 · 16 + 4, we get 3¹⁰⁰ ≡ 3⁴ = 81 ≡ 4 (mod 7). Worked example: 2¹⁰⁰⁰ mod 13. Here φ(13) = 12 and 1000 = 12 · 83 + 4, so 2¹⁰⁰⁰ ≡ 2⁴ = 16 ≡ 3 (mod 13).
The exponent φ(n) is not always the best one. The Carmichael function λ(n) is the exponent of the group (ℤ/nℤ)×, the least m with aᵐ ≡ 1 for all units a. We will compute it in Lesson 6; for instance λ(15) = 4 while φ(15) = 8.
Wilson's theorem
Theorem 4.3 (Wilson; first proved by Lagrange, 1771). An integer n ≥ 2 is prime if and only if (n − 1)! ≡ −1 (mod n).
Proof. Let p be prime. In the field 𝔽ₚ, the equation x² = 1 has only the roots x = ±1 (Theorem 3.3), so every element of {2, 3, …, p − 2} is distinct from its inverse. These elements therefore split into pairs {x, x⁻¹} with product 1, and (p − 1)! ≡ 1 · (p − 1) ≡ −1 (mod p). (For p = 2 the statement is 1 ≡ −1, which is true.) Conversely, if n is composite with a divisor d, 1 < d < n, then d | (n − 1)!, so d cannot divide (n − 1)! + 1, and therefore n cannot either. ∎
Wilson's theorem is a perfect primality criterion and a useless primality test: computing (n − 1)! mod n takes about n multiplications. It is nevertheless a powerful theoretical tool, as the next result shows.
Corollary 4.4. If p ≡ 1 (mod 4), then x = ((p − 1)/2)! satisfies x² ≡ −1 (mod p).
Proof. Pair k with p − k ≡ −k in the product (p − 1)!:
−1 ≡ (p − 1)! = ∏ₖ₌₁(p−1)/2 k(p − k) ≡ (−1)(p−1)/2 (((p − 1)/2)!)² (mod p).
When p ≡ 1 (mod 4) the exponent (p − 1)/2 is even, so the sign (−1)(p−1)/2 is +1 and the congruence reads −1 ≡ (((p − 1)/2)!)². ∎
For p = 13: 6! = 720 = 55 · 13 + 5, and 5² = 25 ≡ −1 (mod 13). Conversely, if x² ≡ −1 (mod p) for an odd prime p, then x has order 4 in 𝔽ₚ×, so 4 | p − 1. Hence −1 is a square modulo an odd prime p if and only if p ≡ 1 (mod 4), a result we will meet again as the first supplementary law of quadratic reciprocity.
Pseudoprimes and Carmichael numbers
The converse of Fermat's theorem is false. 341 = 11 · 31 is composite, yet 2¹⁰ = 1024 = 3 · 341 + 1, so 2³⁴⁰ = (2¹⁰)³⁴ ≡ 1 (mod 341). We call a composite n with an−1 ≡ 1 (mod n) a Fermat pseudoprime to base a. Worse, some composite numbers are pseudoprimes to every coprime base.
Definition 4.5. A Carmichael number is a composite n such that an−1 ≡ 1 (mod n) for every a coprime to n.
Theorem 4.6 (Korselt's criterion, 1899). A composite n is a Carmichael number if and only if n is squarefree and p − 1 | n − 1 for every prime p | n.
Proof of sufficiency. Let a be coprime to n and p | n. Since p − 1 | n − 1, Fermat gives an−1 = (ap−1)(n−1)/(p−1) ≡ 1 (mod p). As n is squarefree it is the product of its distinct prime divisors, so an−1 ≡ 1 modulo their product n (Lesson 5). The converse uses primitive roots and is Problem 57. ∎
The smallest Carmichael number is 561 = 3 · 11 · 17: 560 = 2⁴ · 5 · 7 is divisible by 2, 10 and 16. The next are 1105 = 5 · 13 · 17 and 1729 = 7 · 13 · 19. A Carmichael number is odd and has at least three prime factors. Alford, Granville and Pomerance proved in 1994 that there are infinitely many, indeed more than x2/7 of them up to x for all large x. Carmichael numbers are the reason the Fermat test must be strengthened to the Miller–Rabin test of Lesson 13.
Exercise
- (a)Find the last two digits of 3¹⁰⁰⁰.
- (b)Compute 15! mod 17 and 8! mod 17 using Wilson's theorem.
- (c)Prove that if p is an odd prime, then 1ᵖ⁻¹ + 2ᵖ⁻¹ + ⋯ + (p − 1)ᵖ⁻¹ ≡ −1 (mod p).
Show solution
Answers: (a) φ(100) = 40 and 1000 = 25 · 40, so 3¹⁰⁰⁰ ≡ 1 (mod 100): the last two digits are 01. (b) 16! ≡ −1 and 16 ≡ −1, so 15! ≡ 1 (mod 17). For 8!: 16! = 8! · (9 · 10 ⋯ 16) ≡ 8! · ((−8)(−7)⋯(−1)) = 8! · (−1)⁸ · 8! = (8!)², so (8!)² ≡ −1 (mod 17), which pins 8! down to ±4. Direct computation decides the sign: 8! = 40320 = 2371 · 17 + 13, so 8! ≡ 13 ≡ −4, and indeed (−4)² = 16 ≡ −1. (c) Each of the p − 1 terms is ≡ 1 by Fermat, so the sum is ≡ p − 1 ≡ −1.