Wilson's theorem
Statement
Let \( n \in \mathbb{N} \) with \( n \geq 2 \). Then \( n \) is prime if and only if \[ (n-1)! \equiv -1 \pmod{n}. \] Equivalently, writing \( p \) for a candidate prime: \( p \) is prime exactly when \( (p-1)! + 1 \) is divisible by \( p \). The statement is an "if and only if": both the forward direction (primality implies the congruence) and the converse (the congruence implies primality) hold, which is what makes Wilson's theorem usable as a primality criterion in principle.
Why it matters
Wilson's theorem is the first result a student meets in which a purely multiplicative statement about the group \( (\mathbb{Z}/n\mathbb{Z})^\times \) is converted into a clean numerical congruence, by pairing each unit with its inverse. The proof technique — pairing elements of a finite abelian group with their inverses and isolating the "self-paired" elements — recurs throughout algebra and number theory, from the classification of self-inverse elements in Sylow theory arguments to Gauss's own use of the theorem in his study of quadratic residues.
The theorem also matters because it is a genuine (if computationally useless) primality test: unlike Fermat's little theorem, which only gives a necessary condition for primality, Wilson's theorem gives a necessary and sufficient one. This logical strength — and the fact that \( (n-1)! \) grows too fast to compute for testing large \( n \) — makes it a favourite illustration of the gap between theoretical characterisations and practical algorithms.
Hypotheses
Proof
Direction 1: \( p \) prime \( \implies (p-1)! \equiv -1 \pmod p \).
Direction 2 (converse): \( (n-1)! \equiv -1 \pmod n \implies n \) is prime.
Result
Reading. Multiply together every integer from \( 1 \) up to one less than \( n \); reduce modulo \( n \). You land exactly on \( -1 \) (i.e. \( n-1 \)) precisely when \( n \) has no factors of its own — when it's prime. For any composite \( n \), the same product instead lands on \( 0 \) (or, in the single exceptional case \( n=4 \), on \( 2 \)).
Scope. Holds for all integers \( n \geq 2 \) with no further restriction; it is a full biconditional (necessary and sufficient), unlike Fermat's little theorem which is only necessary for primality. It applies to \( n \) itself, not to intermediate moduli or to factorial-like products truncated elsewhere.
Corollaries & converses
- The converse is not a separate fact to worry about — it is built into the theorem as stated (an "if and only if"), and was proved directly above in Direction 2.
- Corollary (Wilson quotient). For prime \( p \), the integer \( W(p) = \dfrac{(p-1)!+1}{p} \) is a well-defined integer, called the Wilson quotient; its residue mod \( p \) is of independent interest (Wilson primes are those with \( W(p) \equiv 0 \pmod{p^2}\), i.e. \( p^2 \mid (p-1)!+1 \) — only \( 5, 13, 563 \) are known).
- Corollary. For odd prime \( p \), \( \left(\dfrac{p-1}{2}!\right)^2 \equiv (-1)^{(p+1)/2} \pmod p \), obtained by pairing \( k \) with \( p-k \) in \( (p-1)! \); this is a standard route to determining when \( -1 \) is a quadratic residue mod \( p \).
- As a primality test the theorem is correct but practically useless: computing \( (n-1)! \bmod n \) takes \( \Theta(n) \) modular multiplications, vastly slower than trial division or modern probabilistic/deterministic tests (Miller–Rabin, AKS).
Fails without
- Drop \( n \geq 2 \): at \( n = 1 \), \( (0)! = 1 \equiv 0 \pmod 1 \), and the "congruence" \( \equiv -1 \) is vacuously true (everything is \( \equiv 0 \equiv -1 \pmod 1\)) even though \( 1 \) is not prime — the biconditional breaks down because primality is undefined/false at \( n=1\) while the congruence side is trivially satisfied.
- Drop primality of \( p \) in the forward direction (i.e. try to apply the pairing argument to composite \( n \)): for \( n = 6 \), \( \mathbb{Z}/6\mathbb{Z} \) is not a field, \( (\mathbb{Z}/6\mathbb{Z})^\times = \{1,5\} \) has order \( 2 \), not \( 5 \), so the pairing argument over "all nonzero residues" collapses (most residues, e.g. \( 2,3,4\), are zero divisors, not units, and have no inverse); indeed \( 5! = 120 \equiv 0 \pmod 6 \), not \(-1\).
- Forget the exceptional case \( n=4 \) in the converse proof: naively applying the Step 10 pairing (distinct factors \( a \neq b\)) to \( n=4=2\times2\) is impossible since \( a=b=2\), and the Step 11 bound \( 2a \le a^2-1\) fails at \( a=2\) (giving \(4 \le 3\), false); omitting the separate check at \( n=4\) would wrongly suggest \( 3! \equiv 0\), but in fact \( 3!=6\equiv2\pmod4\) — a genuinely different residue that must be checked by hand.
Common errors
- Treating \( (p-1)! \equiv -1 \pmod p \) as an equation over \( \mathbb{Z} \) rather than a congruence, then being confused that \( (p-1)! \) is positive and large while \(-1\) is negative and small (e.g. plugging \( p=7 \): \( 6! = 720 \), and checking "\(720 = -1\)?" instead of "\(720 \equiv -1 \pmod 7\)", i.e. \(721 = 7\times103\)).
- Believing the pairing argument works for composite \( n \) by pairing "all numbers less than \( n \) with their inverses" — forgetting that non-units (zero divisors) exist mod \( n \) when \( n \) is composite and have no multiplicative inverse at all.
- Forgetting to isolate the self-inverse elements \( 1 \) and \( p-1 \) before pairing the rest, and instead trying to pair every element of \( \{1,\dots,p-1\}\) including \(1\) and \(p-1\) with themselves as if they had distinct partners, miscounting the number of pairs.
- Misapplying Wilson's theorem as a practical primality test on large numbers, not registering that computing \( (n-1)! \bmod n \) is computationally far more expensive than simply trial-dividing or applying Miller–Rabin.
- Confusing Wilson's theorem with Fermat's little theorem (\( a^{p-1}\equiv1\pmod p\) for \(\gcd(a,p)=1\)) — the two are related but logically different: Fermat's is only a necessary condition for primality (there are Fermat pseudoprimes), Wilson's is necessary and sufficient.
Discussion
Wilson's theorem is traditionally attributed to John Wilson, a student of Edward Waring who stated it (without proof) in 1770; Waring published it, Lagrange gave the first proof in 1771, and it was later noted that Leibniz had been aware of the essential fact roughly a century earlier without publishing it. The theorem's real content is not really about factorials at all — it is a statement about the structure of the unit group of a finite field, dressed up in factorial notation.
The pairing argument used in Direction 1 is a special case of a much more general fact: in any finite abelian group \( G \), the product of all elements of \( G \) equals the product of all elements of order dividing \( 2 \) (the self-inverse elements), because every other element cancels against its distinct inverse in the full product. Applied to \( G = (\mathbb{Z}/p\mathbb{Z})^\times \), a cyclic group of even order \( p-1 \) (for \( p \) odd), the unique element of order \( 2 \) is \( -1 \), which is exactly what step 5–8 isolates.
This generalisation immediately answers a natural follow-up question: what is the product of all elements of \( (\mathbb{Z}/n\mathbb{Z})^\times \) for composite \( n \)? Gauss showed the product is \( -1 \pmod n \) when \( n \) has a primitive root (i.e. \( n \in \{1,2,4,p^k,2p^k\} \) for odd prime \( p \)), and \( +1 \pmod n \) otherwise — because when no primitive root exists, the group \( (\mathbb{Z}/n\mathbb{Z})^\times \) is non-cyclic and has more than one element of order \( 2\), and these extra involutions pair off and cancel, leaving the identity. This is sometimes called the Gauss–Wilson theorem and is the natural generalisation of Wilson's theorem to composite moduli via unit-group products rather than full factorials.
Common misconception. Students sometimes think Wilson's theorem gives an efficient primality test because it is an "if and only if." Logical strength (necessary and sufficient) says nothing about computational cost: evaluating the criterion requires computing a factorial with \( \Theta(n) \) multiplications, which is exponential in the bit-length of \( n \), making it useless in practice compared to polynomial-time tests such as Miller–Rabin or AKS — a good early lesson that "characterises" and "efficiently decides" are different notions.
Worked examples
Reading. The theorem checks out exactly as predicted for the prime \(p=7\), and the pairing mechanism makes visible why it works rather than just confirming the arithmetic.
Reading. This illustrates the converse being used as intended: a single congruence check, in principle, certifies primality without ever finding a factor — though as discussed, computing \( (n-1)! \) this way is impractical for large \( n \).
Problems
- Verify Wilson's theorem directly (by computing the pairing of inverses) for \( p = 13 \).
Solution
Units are \( \{1,\dots,12\} \). Self-inverse elements are \(1\) and \(12\) (since \(12\equiv-1\)). Find inverse pairs among \( \{2,\dots,11\} \): \(2\cdot7=14\equiv1\); \(3\cdot9=27\equiv1\); \(4\cdot10=40\equiv1\); \(5\cdot8=40\equiv1\)... wait recompute: \(5\cdot8=40\equiv40-39=1\); \(6\cdot11=66\equiv66-65=1\). So pairs are \(\{2,7\},\{3,9\},\{4,10\},\{5,8\},\{6,11\}\) — five pairs covering all ten elements. Then \(12! \equiv 1\cdot(2\cdot7)\cdot(3\cdot9)\cdot(4\cdot10)\cdot(5\cdot8)\cdot(6\cdot11)\cdot12 \equiv 1\cdot1\cdot1\cdot1\cdot1\cdot1\cdot12 \equiv 12 \equiv -1 \pmod{13}\), confirming the theorem.
- Show directly, without invoking the general converse proof, that \( 5! \not\equiv -1 \pmod 6 \), consistent with \( 6 \) being composite.
Solution
\(5! = 120\). \(120 = 6\cdot20 + 0\), so \(5! \equiv 0 \pmod 6\), while \(-1\equiv5\pmod6\). Since \(0\neq5\), the congruence fails, as predicted: \(6=2\cdot3\) with \(2\neq3\) both appearing as separate factors in \(5!=1\cdot2\cdot3\cdot4\cdot5\), giving \(6\mid5!\).
- Using the corollary \( \left(\frac{p-1}{2}!\right)^2 \equiv (-1)^{(p+1)/2}\pmod p \), determine whether \(-1\) is a quadratic residue mod \(13\).
Solution
Here \(p=13\), \((p+1)/2=7\), so the corollary gives \(\left(6!\right)^2\equiv(-1)^7=-1\pmod{13}\). Since \(6! = 720 \equiv 720-55\cdot13=720-715=5\pmod{13}\), check \(5^2=25\equiv25-13=12\equiv-1\pmod{13}\) — consistent. Since \(6!\) squared is \(-1\), \(-1\) is a quadratic residue mod 13 (explicitly, \(5^2\equiv-1\)), matching the general fact that \(-1\) is a QR mod an odd prime \(p\) iff \(p\equiv1\pmod4\), and indeed \(13\equiv1\pmod4\).
- Prove that for every prime \(p\geq5\), \((p-2)!\equiv1\pmod p\).
Solution
By Wilson's theorem, \((p-1)!\equiv-1\pmod p\). Also \((p-1)!=(p-1)\cdot(p-2)!\), and \(p-1\equiv-1\pmod p\), so \((p-1)!\equiv-1\cdot(p-2)!\pmod p\). Combining, \(-1\cdot(p-2)!\equiv-1\pmod p\). Since \(\gcd(-1,p)=1\), \(-1\) is invertible (it is its own inverse), so multiply both sides by \(-1\): \((p-2)!\equiv1\pmod p\).
- A student claims: "since Wilson's theorem is an if-and-only-if, and \(4!=24\equiv0\pmod5\)... wait, \(5\) is prime, so this should be \(-1\)." Find the student's error and correct the computation, then use it to explain why the claim as computed is inconsistent with the theorem — or isn't.
Solution
Recompute: \(4! = 24\). \(24 = 5\cdot4 + 4\), so \(4!\equiv4\pmod5\), and indeed \(4\equiv-1\pmod5\). So there is in fact no inconsistency — the student's arithmetic error was writing "\(\equiv0\)" instead of correctly reducing \(24\bmod5=4\). This exercise is a caution: always finish the modular reduction rather than eyeballing it, since a wrong intermediate residue can make a true theorem look falsified.