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).Wilson's Theorem gives a clean, exact (if-and-only-if) characterization of primality in terms of a factorial congruence. Whereas Fermat's Little Theorem gives a one-directional test that primes pass but some composites also pass (pseudoprimes), Wilson's Theorem fails for every composite, making it a true primality criterion. The surprising fact is that this perfect test exists at all and is so simple to state — yet it is computationally useless for large numbers because computing is far slower than other primality tests. The theorem hinges on the elegant structure of as a field, in which elements pair up with their multiplicative inverses, leaving only the self-inverse elements and unpaired in the giant product.
The Core Insight
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
Wilson's Theorem
Let be an integer with . Then is prime if and only if
Here:
- is the candidate integer;
- is the factorial of ;
- denotes congruence modulo , i.e. .
Equivalently, the divisibility statement is
Converse / Composite Case
If is composite, then
In particular no composite satisfies , which is what makes Wilson's Theorem an exact primality test.
Pairing of inverses mod 11: each element joins its distinct inverse, leaving 1 and 10 self-paired
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 withSo 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 :This proves the forward direction. ∎
Converse: the congruence fails for composites
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 . ∎
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- 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
Verifying Wilson for $p = 7$
Given: (prime). Compute . Steps:- .
- Reduce step by step to avoid big numbers:
- Check pairing: inverses mod 7 are , . Self-inverses are and .
Wilson fails for composite $n = 9$
Given: (composite, ). Compute . Steps:- The factor and the factor both appear in .
- Their product is already a multiple of .
- Therefore , so .
- Since , the Wilson congruence fails.
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:
- Fermat's Little Theorem — both follow from the group structure of and were proved together by Lagrange; Fermat says , Wilson is the product over all .
- Lagrange's Theorem on Polynomial Congruences — a degree- polynomial mod has at most roots; applying this to yields Wilson by comparing constant terms.
- Group Theory — 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).
- Gauss's Generalization — the product of the units mod is if and otherwise.
- Quadratic Residues — Wilson's Theorem yields the value of and Wolstenholme-type identities.
Real-World Applications
While primarily theoretical, Wilson's Theorem and its ideas surface in several concrete contexts:
- Primality proofs in textbooks and competition mathematics — a standard tool for olympiad problems requiring an exact primality characterization.
- Deriving the value of mod — used to compute square roots of modulo primes , relevant in Quadratic Residues and the Tonelli–Shanks algorithm.
- Wilson primes search — distributed computing projects test the rare congruence , probing the boundary of analytic number theory predictions.
- Generating primes in formulas — Wilson's Theorem underlies "prime-producing" formulas (e.g. via the floor function), of theoretical/recreational interest.
- Cryptographic pedagogy — used to introduce modular inverse structure of before RSA and Diffie–Hellman.
- Combinatorial identities — appears in evaluating products and binomial coefficients modulo primes (e.g. Lucas-type congruences).
- Verification of finite field arithmetic — sanity-checking implementations of modular multiplication by confirming .
Historical Development
- ~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 .
- 1801 — Carl Friedrich Gauss, in Disquisitiones Arithmeticae, presents Wilson's Theorem and its generalization to the product of units mod for arbitrary .
- 1828 — Gauss's generalization is fully worked into mainstream number theory, clarifying the cases .
- 1862 — Joseph Wolstenholme proves related higher-order congruences; the notion of Wilson primes (satisfying the congruence mod ) emerges from this lineage.
- 1900s–present — Computational searches identify the only three known Wilson primes: ; none exist below , deepening the mystery of their distribution.
Subtleties & Limits
The Wilson prime mystery. A prime is a Wilson prime if it satisfies the stronger congruenceOnly 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.