Wilson's Theorem
Statement
A natural number is prime if and only if:
The forward direction says primes make the factorial congruent to ; the reverse says any composite fails this test (since some prime factor divides both and , so ).
Visualization
Small cases:
| Prime? | |||
|---|---|---|---|
| 2 | yes | ||
| 3 | yes | ||
| 4 | no (6 ≢ -1≡3) | ||
| 5 | yes | ||
| 6 | no | ||
| 7 | yes | ||
| 11 | yes |
Why pairing works (for prime ):
In , every element has a unique inverse :
1 · 1 = 1 (self-inverse)
2 · 4 = 8 ≡ 1 (pair)
3 · 5 = 15 ≡ 1 (pair)
6 · 6 = 36 ≡ 1 (self-inverse: 6 ≡ -1)
Multiply everything: . ✓
Key insight: the only self-inverse elements mod (prime) are , because .
Proof Sketch
-
Pairing. For prime , each element has a unique inverse (since has only solutions ). These elements pair up.
-
Product of pairs. (each pair contributes a factor of 1).
-
Boundary terms. Include and : the full product .
-
Composite direction. If with , then and , so . Thus (since is a unit).
Connections
Wilson's theorem gives a perfect primality characterisation — but it is computationally impractical (computing is exponential in ). It connects to Fermat's Little TheoremFermat's Little TheoremFor prime p, a^p is congruent to a mod pRead more → (which applies to all elements, not just the factorial) and to Euler's Totient FunctionEuler's Totient Functionφ(n) counts integers up to n coprime to n, and governs modular exponentiationRead more → (the group has order and product for prime ). The pairing argument is also central to Quadratic ReciprocityQuadratic ReciprocityRelationship between solvability of quadratic congruences for two odd primesRead more → proofs. See also Infinitude of PrimesInfinitude of PrimesEuclid's classic proof that there are infinitely many prime numbersRead more → for why primes are abundant enough to make this interesting.
Lean4 Proof
/-- **Wilson's theorem**: for a prime `p`, the factorial `(p-1)!` is congruent
to `-1` in `ZMod p`. Mathlib provides this as `ZMod.wilsons_lemma`. -/
theorem wilson (p : ℕ) [Fact p.Prime] : ((p - 1)! : ZMod p) = -1 :=
ZMod.wilsons_lemma pReferenced by
- Miller–Rabin Primality TestCryptography
- Euler's Totient FunctionNumber Theory
- Lucas's TheoremNumber Theory