Wilson's Theorem

⚡ Key Takeaways

Wilson's Theorem: An integer p>1p > 1 is prime if and only if (p−1)!≡−1(modp)(p-1)! \equiv -1 \pmod{p}. In words, the product of all positive integers less than a number is one short of a multiple of that number exactly when the number is prime. It applies throughout elementary number theory, modular arithmetic, and primality characterization. Conjectured by Ibn al-Haytham (~1000 CE), stated by John Wilson (1770), and first proved by Joseph-Louis Lagrange (1771).

💡 The Core Insight

In the multiplicative group of nonzero residues mod a prime pp, almost every element can be paired with its distinct multiplicative inverse, so those pairs multiply to 11. The only elements equal to their own inverse are 11 and p−1≡−1p-1 \equiv -1. Hence the entire product (p−1)!(p-1)! collapses to 1⋅(−1)⋅(bunch of 1’s)=−11 \cdot (-1) \cdot (\text{bunch of 1's}) = -1. For composites this pairing structure breaks because inverses don't always exist.

Formal Statement

DefinitionWilson's Theorem

Let pp be an integer with p>1p > 1. Then pp is prime if and only if

1
(p−1)!≡−1(modp).\quad (p-1)! \equiv -1 \pmod{p}.

Derivation hint: Read (p−1)!=1⋅2⋅3⋯(p−1)(p-1)! = 1 \cdot 2 \cdot 3 \cdots (p-1) as a product taken in the ring Z/pZ\mathbb{Z}/p\mathbb{Z}; the claim is that this product equals the residue class −1≡p−1-1 \equiv p-1. Here:

  • pp is the candidate integer;
  • (p−1)!=1⋅2⋯(p−1)(p-1)! = 1 \cdot 2 \cdots (p-1) is the factorial of p−1p-1;
  • ≡(modp)\equiv \pmod p denotes congruence modulo pp, i.e. p∣[(p−1)!+1]p \mid \big[(p-1)! + 1\big].

Equivalently, the divisibility statement is

2
p∣(p−1)!+1  ⟺  p is prime.\quad p \mid (p-1)! + 1 \iff p \text{ is prime}.

Derivation hint: a≡−1(modp)a \equiv -1 \pmod p means p∣(a+1)p \mid (a+1); apply with a=(p−1)!a = (p-1)!.

TheoremConverse / Composite Case

If n>1n > 1 is composite, then

3
(n−1)!≡{0(modn),n≠4,2(mod4),n=4.\quad (n-1)! \equiv \begin{cases} 0 \pmod n, & n \neq 4, \\ 2 \pmod 4, & n = 4. \end{cases}

Derivation hint: For composite n>4n > 4, both factors of any factorization n=abn = ab (and enough multiples) appear among 1,…,n−11,\dots,n-1, so n∣(n−1)!n \mid (n-1)!. The number 44 is the lone exception because its only proper factor 22 appears just once below 44. In particular no composite nn satisfies (n−1)!≡−1(modn)(n-1)! \equiv -1 \pmod n, which is what makes Wilson's Theorem an exact primality test.

Figure
Pairing of inverses mod 11: each element joins its distinct inverse, leaving 1 and 10 self-paired
Residues mod 11 and their inverses 1 2 3 4 5 6 7 8 9 10 2·6=12≡1 3·4=12≡1 7·8=56≡1 1·1≡1 10·10≡1 Product = 1·(1)(1)(1)·10 ≡ 10 ≡ -1 (mod 11)

Why It Holds / Derivation

proof[Prime Case: (p−1)!≡−1(modp)(p-1)! \equiv -1 \pmod p] Assume pp is prime. We work in the field Z/pZ\mathbb{Z}/p\mathbb{Z}.

Step 1 — Every nonzero residue is invertible. Since pp is prime, for each a∈{1,2,…,p−1}a \in \{1, 2, \dots, p-1\} we have gcd⁡(a,p)=1\gcd(a,p)=1, so there is a unique a−1∈{1,…,p−1}a^{-1} \in \{1,\dots,p-1\} with

4
a⋅a−1≡1(modp).\quad a \cdot a^{-1} \equiv 1 \pmod p.

Derivation hint: Bézout's identity gives ua+vp=1ua + vp = 1; reduce mod pp to get ua≡1ua \equiv 1, and uniqueness follows from cancellation in the field.

Step 2 — Identify self-inverse elements. An element is its own inverse when a2≡1(modp)a^2 \equiv 1 \pmod p, i.e. p∣(a−1)(a+1)p \mid (a-1)(a+1). As pp is prime it divides one factor, giving

5
a≡1(modp)ora≡−1(modp).\quad a \equiv 1 \pmod p \quad\text{or}\quad a \equiv -1 \pmod p.

Derivation hint: a2−1=(a−1)(a+1)a^2 - 1 = (a-1)(a+1); Euclid's lemma forces pp to divide a−1a-1 or a+1a+1. So only a=1a = 1 and a=p−1a = p-1 (≡−1\equiv -1) satisfy a=a−1a = a^{-1}.

Step 3 — Pair off the remaining factors. The elements {2,3,…,p−2}\{2, 3, \dots, p-2\} (there are p−3p-3 of them, an even count) split into disjoint pairs {a,a−1}\{a, a^{-1}\} with a≠a−1a \neq a^{-1}. Each pair multiplies to 11:

6
∏a=2p−2a≡1(modp).\quad \prod_{a=2}^{p-2} a \equiv 1 \pmod p.

Derivation hint: Reorder the product so each aa stands next to its distinct inverse; every pair contributes a factor a⋅a−1≡1a \cdot a^{-1} \equiv 1, and the whole product is 1(p−3)/2=11^{(p-3)/2} = 1.

Step 4 — Multiply back the unpaired terms. Including 11 and p−1p-1:

7
(p−1)!=1⋅(∏a=2p−2a)⋅(p−1)≡1⋅1⋅(−1)=−1(modp).\quad (p-1)! = 1 \cdot \Big(\prod_{a=2}^{p-2} a\Big) \cdot (p-1) \equiv 1 \cdot 1 \cdot (-1) = -1 \pmod p.

Derivation hint: Substitute the result of Step 3 and use p−1≡−1(modp)p-1 \equiv -1 \pmod p. This proves the forward direction. ∎

ProofConverse: the congruence fails for composites

Suppose n>1n > 1 satisfies (n−1)!≡−1(modn)(n-1)! \equiv -1 \pmod n, and suppose for contradiction nn is composite. Let dd be a proper divisor with 1<d<n1 < d < n. Then dd appears as one of the factors 1,2,…,n−11, 2, \dots, n-1, so d∣(n−1)!d \mid (n-1)!. But d∣nd \mid n and n∣(n−1)!+1n \mid (n-1)! + 1 force d∣(n−1)!+1d \mid (n-1)! + 1. Subtracting, d∣1d \mid 1, a contradiction since d>1d > 1. Hence nn is prime. For the explicit composite values in [3][3]: if n>4n > 4 is composite, write n=abn = ab with 1<a≤b<n1 < a \le b < n. If a≠ba \neq b both appear separately in (n−1)!(n-1)!, so n=ab∣(n−1)!n = ab \mid (n-1)!. If a=ba = b (so n=a2n = a^2), then since n>4n > 4 we have a>2a > 2, and both aa and 2a2a are distinct factors below nn, giving a⋅2a=2a2=2na \cdot 2a = 2a^2 = 2n, so again n∣(n−1)!n \mid (n-1)!. The case n=4n = 4 gives 3!=6≡2(mod4)3! = 6 \equiv 2 \pmod 4. ∎

∎
mathematician · Joseph-Louis Lagrange | 1736–1813

Though the result bears John Wilson's name (Wilson stated it without proof, reportedly relayed by his teacher Edward Waring in 1770), the first rigorous proof was published by Joseph-Louis Lagrange in 1771. Lagrange proved it as a corollary of his deep work on the number of roots of polynomial congruences modulo a prime — establishing that a degree-dd polynomial has at most dd roots mod pp. He even derived both Wilson's Theorem and Fermat's Little Theorem together from this polynomial framework. Lagrange, a towering figure of 18th-century mathematics, made foundational contributions to number theory, the calculus of variations, and analytical mechanics.

Building Intuition

🧠 Intuition

Think of {1,2,…,p−1}\{1, 2, \dots, p-1\} as a dance floor where everyone must pair with their unique "inverse partner" (the person they multiply with to give 11). Because pp is prime, everyone has exactly one partner — no one is left without a match, and no two people share a partner. Two people are their own partner: the identity 11 (trivially) and p−1≡−1p-1 \equiv -1 (since (−1)2=1(-1)^2 = 1). Everyone else dances in genuine couples, and each couple "cancels" to 11. So the grand product is just the two wallflowers: 1×(−1)=−11 \times (-1) = -1. For a composite nn, the floor is broken: some elements have no inverse at all (those sharing a factor with nn), so the clean pairing collapses, and the product instead gets dragged down to 00 because the composite's factors are all sitting in the product waiting to assemble nn itself.

Worked Examples

ExampleVerifying Wilson for p=7p = 7

Given: p=7p = 7 (prime). Compute (7−1)!=6!(mod7)(7-1)! = 6! \pmod 7.

Steps: 1. 6!=7206! = 720. 2. Reduce step by step to avoid big numbers:

  • 1⋅2=21 \cdot 2 = 2
  • 2⋅3=62 \cdot 3 = 6
  • 6⋅4=24≡3(mod7)6 \cdot 4 = 24 \equiv 3 \pmod 7
  • 3⋅5=15≡1(mod7)3 \cdot 5 = 15 \equiv 1 \pmod 7
  • 1⋅6=6≡−1(mod7)1 \cdot 6 = 6 \equiv -1 \pmod 7

3. Check pairing: inverses mod 7 are 2⋅4=8≡12\cdot4=8\equiv1, 3⋅5=15≡13\cdot5=15\equiv1. Self-inverses are 11 and 66.

Answer: 6!≡−1(mod7)\boxed{6! \equiv -1 \pmod 7} — confirming 77 is prime.

ExampleWilson fails for composite n=9n = 9

Given: n=9n = 9 (composite, =32= 3^2). Compute (9−1)!=8!(mod9)(9-1)! = 8! \pmod 9.

Steps: 1. The factor 33 and the factor 66 both appear in 8!=1⋅2⋯88! = 1\cdot2\cdots8. 2. Their product 3⋅6=18=2⋅93 \cdot 6 = 18 = 2 \cdot 9 is already a multiple of 99. 3. Therefore 9∣8!9 \mid 8!, so 8!≡0(mod9)8! \equiv 0 \pmod 9. 4. Since 0≢−1≡8(mod9)0 \not\equiv -1 \equiv 8 \pmod 9, the Wilson congruence fails.

Answer: 8!≡0(mod9)≠−1\boxed{8! \equiv 0 \pmod 9 \neq -1} — correctly flagging 99 as composite (consistent with equation [3][3]).

Common Misconceptions

⚠️ Common mistake

Wilson's Theorem is NOT a practical primality test. Computing (p−1)!(p-1)! requires ∼p\sim p multiplications, which is exponential in the number of digits of pp. For real primality testing one uses Fermat's Little Theorem-based tests, the Miller–Rabin test, or AKS. Wilson's Theorem is theoretically perfect but computationally hopeless for large pp.

Do not confuse the directions. Some students remember only "(p−1)!≡−1(p-1)! \equiv -1 for primes" and forget that the converse also holds — composites never satisfy it (except the n=4n=4 residue quirk, which still gives 22, not −1-1). The biconditional is the whole point.

Counterexample

A tempting false analogy: One might guess that (n−1)!≡−1(modn)(n-1)! \equiv -1 \pmod n has "near misses" for composites, like Fermat pseudoprimes do for Fermat's Little Theorem. It does not. Take n=15n = 15: the factors 33 and 55 both lie in 14!14!, so 15∣14!15 \mid 14!, giving 14!≡0(mod15)14! \equiv 0 \pmod{15}, nowhere near −1≡14-1 \equiv 14. There are no "Wilson pseudoprimes" — the test has zero false positives. (Separately, a Wilson prime is a much rarer beast: a prime pp with (p−1)!≡−1(modp2)(p-1)! \equiv -1 \pmod{p^2}; only 5,13,5635, 13, 563 are known.)

Connections

connection

Wilson's Theorem sits in a tight web of elementary number theory:

  • <a class="wiki-link" href="/wiki/math-numthy-fermat-little-theorem">Fermat&#39;s Little Theorem</a> — both follow from the group structure of (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times and were proved together by Lagrange; Fermat says ap−1≡1a^{p-1}\equiv 1, Wilson is the product over all aa.
  • <span class="wiki-link-missing">Lagrange&#39;s Theorem on Polynomial Congruences</span> — a degree-dd polynomial mod pp has at most dd roots; applying this to xp−1−1x^{p-1}-1 yields Wilson by comparing constant terms.
  • <span class="wiki-link-missing">Group Theory</span> — Wilson generalizes: in any finite abelian group, the product of all elements equals the product of elements of order ≤2\le 2 (the identity unless there is a unique element of order 2).
  • <span class="wiki-link-missing">Gauss&#39;s Generalization</span> — the product of the units mod nn is −1-1 if n∈{4,pk,2pk}n \in \{4, p^k, 2p^k\} and +1+1 otherwise.
  • <span class="wiki-link-missing">Quadratic Residues</span> — Wilson's Theorem yields the value of (−1p)\big(\frac{-1}{p}\big) and Wolstenholme-type identities.

Real-World Applications

🔬 Where this appears

Historical Development

⏳ History
- ~1000 CE — Ibn al-Haytham (Alhazen) implicitly states the result in his study of congruences, the earliest known appearance.
- 1770 — John Wilson, a student of Edward Waring, conjectures the theorem; Waring publishes it in Meditationes Algebraicae without proof, remarking that proofs may be difficult because there is no good notation for primes.
- 1771 — Joseph-Louis Lagrange publishes the first proof, derived from his theory of polynomial congruences, and notes the converse.
- 1773 — Leonhard Euler gives an alternative proof using primitive roots and the structure of (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times.
- 1801 — Carl Friedrich Gauss, in Disquisitiones Arithmeticae, presents Wilson's Theorem and its generalization to the product of units mod nn for arbitrary nn.
- 1828 — Gauss's generalization is fully worked into mainstream number theory, clarifying the cases 4,pk,2pk4, p^k, 2p^k.
- 1862 — Joseph Wolstenholme proves related higher-order congruences; the notion of Wilson primes (satisfying the congruence mod p2p^2) emerges from this lineage.
- 1900s–present — Computational searches identify the only three known Wilson primes: 5,13,5635, 13, 563; none exist below 2×10132 \times 10^{13}, deepening the mystery of their distribution.

Subtleties & Limits

open-problem

The Wilson prime mystery. A prime pp is a Wilson prime if it satisfies the stronger congruence

8
(p−1)!≡−1(modp2).\quad (p-1)! \equiv -1 \pmod{p^2}.

Derivation hint: This strengthens equation [1][1] from modulus pp to modulus p2p^2, requiring p2∣(p−1)!+1p^2 \mid (p-1)! + 1 rather than merely p∣(p−1)!+1p \mid (p-1)! + 1. Only three are known: 5,13,5635, 13, 563. Heuristic arguments (modeling (p−1)!+1p  p\frac{(p-1)!+1}{p} \bmod p as uniformly random) predict there should be infinitely many, with roughly log⁡log⁡X\log\log X of them below XX — an excruciatingly slow growth that explains why so few are found. It is unknown whether there are infinitely many Wilson primes, and it is even unknown whether there are infinitely many non-Wilson primes (though the latter is widely believed). Extensive searches confirm no fourth Wilson prime exists up to about 2×10132 \times 10^{13}. This remains a genuinely open problem at the intersection of elementary and analytic number theory.

See Also

Fermat's Little Theorem · Modular Arithmetic · Multiplicative Inverses Modulo n · Gauss's Generalization · Quadratic Residues · Euclid's Lemma · Lagrange's Theorem on Polynomial Congruences · Wilson Primes · Primality Testing · Group Theory