Wilson's Theorem
Take a prime number . Multiply together all the integers from to . Ask what remainder this product leaves upon division by . For : , so . This is not a coincidence of . It happens for every prime. Remarkably, it happens for no composite number greater than . This single congruence, , is Wilson's Theorem. Its converse gives a genuine (if computationally impractical) primality test. A number is prime if and only if this factorial congruence holds. The result matters because it is one of the first places in elementary number theory. Here, the multiplicative structure of reveals itself through pure counting and pairing arguments. No appeal to primitive roots or deep machinery is needed. It sits alongside Fermat's Little Theorem as a foundational fact. Together they show how the nonzero residues modulo a prime organize themselves. Its proof technique — pairing each element with its modular inverse — is a template. This template is used throughout multiplicative number theory. The big picture is that the group is cyclic of order . Wilson's theorem is the shadow this cyclicity casts onto the humble factorial. Every element pairs off with its inverse. The exceptions are the two elements that are their own inverses, and . Multiplying everything collapses to . This article proves the theorem rigorously via this pairing argument. It also proves the converse and works two nontrivial numerical examples. It shows exactly why compositeness breaks the congruence. Finally, it points toward how this circle of ideas foreshadows Primitive Roots. This article delivers a complete, self-contained treatment. It includes the precise statement and a full proof of both directions. It offers a worked computation for a two-digit prime. It also works a composite number illustrating the failure. There is a counterexample showing why "prime" cannot be weakened. It discusses edge cases as well. Finally, it gives historical context connecting Wilson, Waring, Lagrange, and Gauss.
- Wilson's Theorem: for prime , .
- The converse also holds: if then is prime, giving an iff characterization.
- The proof pairs each residue with its unique modular inverse in .
- Only and are self-inverse, so all other factors cancel in pairs to .
- Composite fails the congruence because actually contains a factor of (or enough of its factors), forcing .
- Key idea: the factorial secretly computes the product of all elements of the cyclic group , and that product is always the unique order-2 element .
Where this lives today
Wilson's Theorem is primarily a conceptual cornerstone rather than a computational workhorse, but its ideas ramify widely. In algebra and group theory, the proof technique — pairing group elements with their inverses to compute the product of all elements of a finite abelian group — generalizes far beyond ; Gauss's generalization computes this product for any finite abelian group, tying Wilson's theorem to the classification of units modulo and directly motivating the study of Primitive Roots and cyclic groups of units. In combinatorics and enumerative number theory, Wilson's theorem underlies identities involving factorials modulo primes, such as Wilson quotients , whose divisibility properties (Wilson primes, where ) remain an active area of computational search — only three are known (, , ) as of extensive searches. In cryptography and algorithmic number theory, while Wilson's theorem itself is too slow to be a practical primality test, it appears pedagogically as the first example students see of a "primality certificate" phrased purely in terms of a congruence, foreshadowing more efficient certificate-based and probabilistic tests (Fermat, Miller–Rabin, AKS) built on the same underlying philosophy: primes are characterized by algebraic congruences that composites almost always violate. In combinatorial identities and formal power series, factorial congruences modulo primes feed into the study of -adic Gamma functions, where Wilson's theorem is precisely the statement that the -adic Gamma function satisfies in an appropriate normalization, linking elementary number theory to -adic analysis.
Ask the tutor: How does the inverse-pairing argument in this proof change when we move from to the unit group of a general finite abelian group, and what exactly makes primitive roots the right next concept to study?
Let be a positive integer and let denote the product (with the convention , so the statement is vacuous/trivial to check for ). We say satisfies Wilson's congruence if
Wilson's Theorem asserts that satisfies this congruence if and only if is prime (with the degenerate case , where , holding trivially since every congruence holds mod ; the substantive content is for ).
Intuition
Think of the nonzero residues modulo a prime as points on a clock with hours, arranged not by size but by multiplicative pairing. The key algebraic fact, already available from Modular Inverses, is that every with has a unique in the same range with . If we form the product , we can group each factor together with its inverse, and each such pair multiplies to — unless an element is its own inverse. Solving shows exactly two such self-paired elements exist: and . Every other residue joins a distinct partner to cancel to . What survives the cancellation is only the product of the unpaired elements: . The diagram below shows this pairing for : the ten nonzero residues split into four inverse-pairs (each pair shown as a chord connecting to ) plus the two fixed points and .
The picture makes the theorem almost visually obvious: cancel the chords (each worth ), and only the two isolated points and remain, multiplying to . This is the entire proof in geometric form; the algebra below simply makes the pairing and the fixed-point count rigorous.
Let be a prime number. Then
Conversely, let be an integer with . If
then is prime. Hence for every integer : is prime .
Let be prime and let . Then if and only if or .
() If , then . If , then .
() Suppose , i.e. . Since is prime, is a prime element of (Euclid's lemma: if a prime divides a product it divides one of the factors). Hence
If , then since we have , and the only multiple of in this range is , forcing , i.e. . If , then , and the only multiple of in this range is itself, forcing , i.e. .
∎We give the proof of the full theorem in two directions, using elementary group-theoretic pairing (the technique is sometimes called the inverse-pairing argument).
Direction 1: prime . Base cases. For : since . For : . Both check directly; assume now (the argument below in fact also covers but the pairing is vacuous/degenerate there, so we isolate them for clarity). Setup. Since is prime, every is coprime to (as has no divisors other than and , and ). By the theory of Modular Inverses, for each such there is a unique with
(Existence and uniqueness of follow from Bézout's identity: since , there exist integers with , so ; reducing modulo into the range gives , and uniqueness holds because if then , and both lie in so .) Fixed points. By the Lemma above, (i.e., is its own inverse) exactly when or . Pairing off the rest. Consider the set , which has elements (an even number, since is odd for ; indeed is even iff is odd, true for all odd primes). The map is an involution on : it sends into , and for (since ), and either — because if then forces , and if then , forcing . So the involution maps to itself, has no fixed points (since only at , both outside ), and is its own inverse (applying it twice returns , since ). An involution without fixed points on a finite set partitions that set into disjoint -element orbits ; hence splits into pairs, each satisfying . Assembling the product. Now compute:
Each of the pair-products is , so the middle product is . Therefore
This proves the forward direction for all primes .
Direction 2: prime (for ). We prove the contrapositive: if is composite, then ; in fact we show except in one explicit small case, and in all cases . Case A: . Direct check: , and . So the congruence fails for . Case B: composite, , so or has a nontrivial factorization we can exploit; we split further. Since is composite, write with (take to be the smallest divisor of greater than , so is prime and ). Sub-case B1: . Then and are two distinct integers, both lying in (since ), so both appear as separate factors in the product . Hence divides , giving
Sub-case B2: , i.e. with prime. If , then since (as ), both and are distinct integers lying in (indeed exactly when ). So contains both and as distinct factors, hence contains as a divisor of the product, so certainly , giving again . If , then , which is exactly Case A, already handled separately (there , so the argument of B2 for does not apply, which is precisely why is the genuine exception). Conclusion of Direction 2. For every composite : if , ; if , , and since (the smallest composite other than ) we have (as that would require , impossible for ). In every composite case the Wilson congruence fails. Equivalently, if holds, cannot be composite; combined with the trivial fact that is not prime and is excluded by hypothesis , must be prime. Both directions are established, completing the proof of Wilson's Theorem in its full iff form.
∎Worked examples
Example 1: Verifying Wilson's Theorem for . We compute using the pairing method directly, rather than brute-force multiplying , to illustrate the proof's mechanism. We need inverses of modulo (excluding the fixed points and ):
- , so .
- (since ), so .
- (since ), so .
- , so .
- (since ), so .
This accounts for all of in five pairs: — indeed pairs as the theorem predicts. Each pair multiplies to , so
As a sanity check against direct computation: . Dividing, , since and . Indeed , confirming the pairing computation exactly.
Example 2: Testing the converse on a composite number, , and locating exactly where it fails. Here , with distinct and both less than . Sub-case B1 of the proof applies directly: both and occur as separate factors among in the product , so
Concretely, contains the factor (from the term "3") and the factor (from the term "5") as genuinely separate multiplicands, so divides the product; hence . We can double check with a partial direct reduction. Modulo : . Already (since ), so as soon as we multiply through the term , the running product is , and multiplying by anything more keeps it . Hence
So decisively fails Wilson's congruence, exactly as the converse direction predicts for composite , and this failure is not a near-miss — the residue is , as far from as a residue can be relative to divisibility structure, since and differ by , coprime-flavored evidence that no small perturbation fixes it.
The forward direction of Wilson's Theorem crucially uses that every element of is invertible modulo — true only when is prime, since invertibility mod is equivalent to coprimality with . If we try to run the same pairing argument with composite, it breaks down at the very first step, and the theorem's conclusion genuinely fails. Take (composite, not prime). The residues are not all coprime to : specifically and share a factor of with , so and have no multiplicative inverse modulo at all. The pairing argument's foundational step — "every has a unique inverse " — is simply false for or here; there is no with , because for every , never . Consequently the entire cancellation-to- mechanism collapses. Concretely, . Since contains both factors and (and also 's prime factor appears again via the factor ""), we get and in fact directly (as ). Explicitly, exactly, so
This shows precisely why primality is not just a convenient hypothesis but the load-bearing one: drop it, and the group-of-units structure no longer includes all of , some factors become zero-divisors, the product is forced toward , and the clean pairing-to- argument has no analogue to fall back on.
Scope, edge cases & common traps
| Case | What happens | Why |
|---|---|---|
| Congruence holds trivially (everything is ) | Degenerate modulus; usually excluded from the "iff prime" statement by convention | |
| Smallest prime; pairing argument is vacuous () but base case checks directly | ||
| , fails Wilson but is the unique composite exception to | is too small for the two-distinct-multiples trick (needs with ); direct check needed | |
| composite, | , never | Two distinct proper factors of (or a repeated small prime with room to spare) both occur inside |
| prime, large | is astronomically large; congruence still holds but is computationally useless as a primality test | Factorial growth makes this test worse than trial division for all practical |
The mathematician behind it
Origins · 1 minJohn Wilson and how this was discovered
John Wilson studied at Cambridge, won the title of Senior Wrangler in 1761, and later became a judge; his mathematical career was brief; he communicated the observation that bears his name to his teacher Edward Waring, who published it without proof in his 1770 treatise Meditationes Algebraicae. Wilson himself never published a proof and did not pursue mathematics as a profession.
Does Wilson's Theorem give a fast way to test primality in practice?
No. Although the theorem gives a mathematically exact characterization of primes, computing requires on the order of modular multiplications if done naively, which is drastically slower than trial division (about divisions) or modern probabilistic tests like Miller–Rabin (polynomial in ). Wilson's theorem is prized for its elegance and its role in illustrating the structure of , not for computational primality testing. It is, however, historically and pedagogically important as one of the earliest exact algebraic characterizations of primality, predating the probabilistic and deterministic polynomial-time tests developed centuries later.
Why does the proof need to be prime rather than just "coprime to something"?
Primality of guarantees that every nonzero residue is invertible modulo , because coprimality with holds automatically for all integers strictly between and precisely when has no divisors besides and itself. This universal invertibility is what allows the pairing argument to partition the entire set into inverse pairs with no leftover elements. If were composite, some residues would be zero-divisors with no inverse, the pairing would be incomplete, and — as the counterexample with shows — the product collapses toward rather than .
Is the converse direction really necessary, or is it "obviously true" once the forward direction is known?
It is genuinely necessary and does not follow automatically. The forward direction says primes satisfy the congruence; by itself this says nothing about whether composites might also happen to satisfy it. The converse is a separate existence claim about composite numbers failing the congruence, proved here by exhibiting two distinct factors of (or an explicit repeated-factor argument) that force to be divisible by , hence congruent to , never (except the isolated case , checked directly). Skipping this direction is one of the most common incomplete "proofs" of Wilson's Theorem found in casual treatments.
What is a Wilson prime, and why is it interesting?
A Wilson prime is a prime satisfying the stronger congruence , i.e., the Wilson quotient is itself divisible by . Only three are currently known: , discovered by extensive computer search, and it remains an open question whether infinitely many exist or whether these three are the only ones below any given (very large) bound. This mirrors the analogous open question for Wieferich primes in Fermat's little theorem, showing how a clean elementary congruence can hide deep unsolved arithmetic behind a stronger modulus.
How does Wilson's Theorem relate to Fermat's Little Theorem?
Both describe the multiplicative structure of for prime , but from different angles. Fermat's Little Theorem says every nonzero residue satisfies , a statement about the order of individual elements dividing . Wilson's Theorem instead computes the product of all elements at once, revealing it equals . They are logically independent as stated (neither proof requires the other here), though both ultimately reflect the same underlying fact: is a group of order , and this article's proof of Wilson's theorem uses only elementary inverse-pairing, not the full cyclic structure exploited in deeper treatments via Primitive Roots.
Can Wilson's Theorem be generalized to non-prime moduli?
Yes — this is Gauss's generalization. For any integer , consider the product of all elements of (the residues coprime to , not all residues). Gauss showed if has a primitive root (i.e., for odd prime ), and otherwise. This recovers Wilson's Theorem exactly when is prime (where every residue is a unit) and shows the phenomenon is really about the structure of the unit group, foreshadowing the theory of Primitive Roots.
Why does the pairing argument fail to directly give a formula for the Wilson quotient's value modulo higher powers of ?
The pairing argument only tracks residues modulo itself; it shows each inverse pair multiplies to something congruent to , but says nothing about how far from exactly that product is (i.e., its value mod or higher). Determining (the Wilson quotient) requires finer tools — such as properties of the -adic Gamma function or explicit symmetric function identities among inverses — well beyond the elementary counting argument used here, which is precisely why Wilson primes are hard to find and their infinitude is open.