Wilson's Theorem
Wilson's Theorem: An integer is prime if and only if . 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).
In the multiplicative group of nonzero residues mod a prime , almost every element can be paired with its distinct multiplicative inverse, so those pairs multiply to . The only elements equal to their own inverse are and . Hence the entire product collapses to . For composites this pairing structure breaks because inverses don't always exist.
Formal Statement
Let be an integer with . Then is prime if and only if
Derivation hint: Read as a product taken in the ring ; the claim is that this product equals the residue class . Here:
- is the candidate integer;
- is the factorial of ;
- denotes congruence modulo , i.e. .
Equivalently, the divisibility statement is
Derivation hint: means ; apply with .
If is composite, then
Derivation hint: For composite , both factors of any factorization (and enough multiples) appear among , so . The number is the lone exception because its only proper factor appears just once below . In particular no composite satisfies , which is what makes Wilson's Theorem an exact primality test.
Why It Holds / Derivation
proof[Prime Case: ] Assume is prime. We work in the field .
Step 1 — Every nonzero residue is invertible. Since is prime, for each we have , so there is a unique with
Derivation hint: Bézout's identity gives ; reduce mod to get , and uniqueness follows from cancellation in the field.
Step 2 — Identify self-inverse elements. An element is its own inverse when , i.e. . As is prime it divides one factor, giving
Derivation hint: ; Euclid's lemma forces to divide or . So only and () satisfy .
Step 3 — Pair off the remaining factors. The elements (there are of them, an even count) split into disjoint pairs with . Each pair multiplies to :
Derivation hint: Reorder the product so each stands next to its distinct inverse; every pair contributes a factor , and the whole product is .
Step 4 — Multiply back the unpaired terms. Including and :
Derivation hint: Substitute the result of Step 3 and use . This proves the forward direction. ∎
Suppose satisfies , and suppose for contradiction is composite. Let be a proper divisor with . Then appears as one of the factors , so . But and force . Subtracting, , a contradiction since . Hence is prime. For the explicit composite values in : if is composite, write with . If both appear separately in , so . If (so ), then since we have , and both and are distinct factors below , giving , so again . The case gives . ∎
∎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- polynomial has at most roots mod . 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
Think of as a dance floor where everyone must pair with their unique "inverse partner" (the person they multiply with to give ). Because 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 (trivially) and (since ). Everyone else dances in genuine couples, and each couple "cancels" to . So the grand product is just the two wallflowers: . For a composite , the floor is broken: some elements have no inverse at all (those sharing a factor with ), so the clean pairing collapses, and the product instead gets dragged down to because the composite's factors are all sitting in the product waiting to assemble itself.
Worked Examples
Given: (prime). Compute .
Steps: 1. . 2. Reduce step by step to avoid big numbers:
3. Check pairing: inverses mod 7 are , . Self-inverses are and .
Answer: — confirming is prime.
Given: (composite, ). Compute .
Steps: 1. The factor and the factor both appear in . 2. Their product is already a multiple of . 3. Therefore , so . 4. Since , the Wilson congruence fails.
Answer: — correctly flagging as composite (consistent with equation ).
Common Misconceptions
Wilson's Theorem is NOT a practical primality test. Computing requires multiplications, which is exponential in the number of digits of . 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 .
Do not confuse the directions. Some students remember only " for primes" and forget that the converse also holds — composites never satisfy it (except the residue quirk, which still gives , not ). The biconditional is the whole point.
A tempting false analogy: One might guess that has "near misses" for composites, like Fermat pseudoprimes do for Fermat's Little Theorem. It does not. Take : the factors and both lie in , so , giving , nowhere near . There are no "Wilson pseudoprimes" — the test has zero false positives. (Separately, a Wilson prime is a much rarer beast: a prime with ; only are known.)
Connections
Wilson's Theorem sits in a tight web of elementary number theory:
- <a class="wiki-link" href="/wiki/math-numthy-fermat-little-theorem">Fermat's Little Theorem</a> — both follow from the group structure of and were proved together by Lagrange; Fermat says , Wilson is the product over all .
- <span class="wiki-link-missing">Lagrange's Theorem on Polynomial Congruences</span> — a degree- polynomial mod has at most roots; applying this to 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 (the identity unless there is a unique element of order 2).
- <span class="wiki-link-missing">Gauss's Generalization</span> — the product of the units mod is if and otherwise.
- <span class="wiki-link-missing">Quadratic Residues</span> — Wilson's Theorem yields the value of and Wolstenholme-type identities.
Real-World Applications
Historical Development
Subtleties & Limits
The Wilson prime mystery. A prime is a Wilson prime if it satisfies the stronger congruence
Derivation hint: This strengthens equation from modulus to modulus , requiring rather than merely . Only three are known: . Heuristic arguments (modeling as uniformly random) predict there should be infinitely many, with roughly of them below — 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 . 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