Wilson's Theorem

lean4-proofnumber-theoryprimalityfactorialsvisualization
(p1)!1(modp)(p-1)! \equiv -1 \pmod{p}

Statement

A natural number p>1p > 1 is prime if and only if:

(p1)!1(modp)(p-1)! \equiv -1 \pmod{p}

The forward direction says primes make the factorial congruent to 1-1; the reverse says any composite nn fails this test (since some prime factor q<nq < n divides both nn and (n1)!(n-1)!, so (n1)!≢1(modn)(n-1)! \not\equiv -1 \pmod{n}).

Visualization

Small cases:

pp(p1)!(p-1)!(p1)!modp(p-1)! \bmod pPrime?
21!=11! = 1111 \equiv -1yes
32!=22! = 2212 \equiv -1yes
43!=63! = 6626 \equiv 2no (6 ≢ -1≡3)
54!=244! = 2424124 \equiv -1yes
65!=1205!=1201200120 \equiv 0no
76!=7206!=7207201720 \equiv -1yes
1110!10!362880013628800 \equiv -1yes

Why pairing works (for prime p=7p = 7):

In Z/7Z\mathbb{Z}/7\mathbb{Z}, every element 1a51 \le a \le 5 has a unique inverse a1aa^{-1} \ne a:

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: 6!=1(24)(35)6111(1)=1(mod7)6! = 1 \cdot (2 \cdot 4) \cdot (3 \cdot 5) \cdot 6 \equiv 1 \cdot 1 \cdot 1 \cdot (-1) = -1 \pmod 7. ✓

Key insight: the only self-inverse elements mod pp (prime) are ±1\pm 1, because a21    p(a1)(a+1)    a±1a^2 \equiv 1 \implies p \mid (a-1)(a+1) \implies a \equiv \pm 1.

Proof Sketch

  1. Pairing. For prime pp, each element a{2,,p2}a \in \{2, \ldots, p-2\} has a unique inverse a1aa^{-1} \ne a (since a21(modp)a^2 \equiv 1 \pmod p has only solutions a±1a \equiv \pm 1). These elements pair up.

  2. Product of pairs. a=2p2a1(modp)\prod_{a=2}^{p-2} a \equiv 1 \pmod p (each pair contributes a factor of 1).

  3. Boundary terms. Include 11 and p11p-1 \equiv -1: the full product (p1)!11(1)=1(modp)(p-1)! \equiv 1 \cdot 1 \cdot (-1) = -1 \pmod p.

  4. Composite direction. If n=abn = ab with 1<a,b<n1 < a, b < n, then a(n1)!a \mid (n-1)! and ana \mid n, so agcd((n1)!,n)a \mid \gcd((n-1)!, n). Thus (n1)!≢1(modn)(n-1)! \not\equiv -1 \pmod n (since 1-1 is a unit).

Connections

Wilson's theorem gives a perfect primality characterisation — but it is computationally impractical (computing (p1)!(p-1)! is exponential in pp). It connects to Fermat's Little TheoremFermat's Little Theoremapa(modp)a^p \equiv a \pmod{p}For 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 Functionaφ(n)1(modn)a^{\varphi(n)} \equiv 1 \pmod{n}φ(n) counts integers up to n coprime to n, and governs modular exponentiationRead more → (the group (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times has order p1p-1 and product 1-1 for prime pp). The pairing argument is also central to Quadratic ReciprocityQuadratic Reciprocity(pq)(qp)=(1)p12q12\left(\frac{p}{q}\right)\left(\frac{q}{p}\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}}Relationship between solvability of quadratic congruences for two odd primesRead more → proofs. See also Infinitude of PrimesInfinitude of PrimesnN,  p>n  such that  p  is prime\forall\, n \in \mathbb{N},\; \exists\, p > n \;\text{such that}\; p \;\text{is prime}Euclid'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 p