Wilson's Theorem

Take a prime number pp. Multiply together all the integers from 11 to p−1p-1. Ask what remainder this product leaves upon division by pp. For p=7p = 7: 6!=720=7⋅102+66! = 720 = 7 \cdot 102 + 6, so 6!≡6≡−1(mod7)6! \equiv 6 \equiv -1 \pmod 7. This is not a coincidence of 77. It happens for every prime. Remarkably, it happens for no composite number greater than 44. This single congruence, (p−1)!≡−1(modp)(p-1)! \equiv -1 \pmod p, 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 (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times 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 (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times is cyclic of order p−1p-1. 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, 11 and −1-1. Multiplying everything collapses to 1⋅(−1)=−11 \cdot (-1) = -1. 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.

Key takeaways
  • Wilson's Theorem: for prime pp, (p−1)!≡−1(modp)(p-1)! \equiv -1 \pmod p.
  • The converse also holds: if (n−1)!≡−1(modn)(n-1)! \equiv -1 \pmod n then nn is prime, giving an iff characterization.
  • The proof pairs each residue 1,…,p−11, \dots, p-1 with its unique modular inverse in (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times.
  • Only 11 and p−1≡−1p-1 \equiv -1 are self-inverse, so all other factors cancel in pairs to 11.
  • Composite n>4n > 4 fails the congruence because (n−1)!(n-1)! actually contains a factor of nn (or enough of its factors), forcing (n−1)!≡0(modn)(n-1)! \equiv 0 \pmod n.
  • Key idea: the factorial (p−1)!(p-1)! secretly computes the product of all elements of the cyclic group (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times, and that product is always the unique order-2 element −1-1.

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 (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times; Gauss's generalization computes this product for any finite abelian group, tying Wilson's theorem to the classification of units modulo nn 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 W(p)=(p−1)!+1pW(p) = \frac{(p-1)!+1}{p}, whose divisibility properties (Wilson primes, where p2∣(p−1)!+1p^2 \mid (p-1)!+1) remain an active area of computational search — only three are known (55, 1313, 563563) 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 pp-adic Gamma functions, where Wilson's theorem is precisely the statement that the pp-adic Gamma function satisfies Γp(0)=−1\Gamma_p(0)=-1 in an appropriate normalization, linking elementary number theory to pp-adic analysis.

Ask the tutor: How does the inverse-pairing argument in this proof change when we move from (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times to the unit group of a general finite abelian group, and what exactly makes primitive roots the right next concept to study?

DefinitionWilson's Theorem — statement of the congruence

Let nn be a positive integer and let (n−1)!(n-1)! denote the product 1⋅2⋯(n−1)1 \cdot 2 \cdots (n-1) (with the convention 0!=10! = 1, so the statement is vacuous/trivial to check for n=1n=1). We say nn satisfies Wilson's congruence if

(n−1)!≡−1(modn).(n-1)! \equiv -1 \pmod n.

Wilson's Theorem asserts that nn satisfies this congruence if and only if nn is prime (with the degenerate case n=1n=1, where (n−1)!=0!=1≡0≡−1(mod1)(n-1)! = 0! = 1 \equiv 0 \equiv -1 \pmod 1, holding trivially since every congruence holds mod 11; the substantive content is for n≥2n \ge 2).

Intuition

Think of the nonzero residues modulo a prime pp as points on a clock with p−1p-1 hours, arranged not by size but by multiplicative pairing. The key algebraic fact, already available from Modular Inverses, is that every aa with 1≤a≤p−11 \le a \le p-1 has a unique a−1a^{-1} in the same range with a⋅a−1≡1(modp)a \cdot a^{-1} \equiv 1 \pmod p. If we form the product 1⋅2⋯(p−1)1 \cdot 2 \cdots (p-1), we can group each factor together with its inverse, and each such pair multiplies to 11 — unless an element is its own inverse. Solving a2≡1(modp)a^2 \equiv 1 \pmod p shows exactly two such self-paired elements exist: a=1a = 1 and a=p−1a = p - 1. Every other residue joins a distinct partner to cancel to 11. What survives the cancellation is only the product of the unpaired elements: 1⋅(p−1)≡−11 \cdot (p-1) \equiv -1. The diagram below shows this pairing for p=11p = 11: the ten nonzero residues split into four inverse-pairs (each pair shown as a chord connecting aa to a−1a^{-1}) plus the two fixed points 11 and 1010.

The picture makes the theorem almost visually obvious: cancel the chords (each worth 11), and only the two isolated points 11 and 10≡−110 \equiv -1 remain, multiplying to −1-1. This is the entire proof in geometric form; the algebra below simply makes the pairing and the fixed-point count rigorous.

TheoremWilson's Theorem

Let pp be a prime number. Then

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

Conversely, let nn be an integer with n≥2n \ge 2. If

(n−1)!≡−1(modn),(n-1)! \equiv -1 \pmod n,

then nn is prime. Hence for every integer n≥2n \ge 2: nn is prime   ⟺  (n−1)!≡−1(modn)\iff (n-1)! \equiv -1 \pmod n.

LemmaSelf-inverse elements mod a prime

Let pp be prime and let a∈{1,2,…,p−1}a \in \{1, 2, \dots, p-1\}. Then a2≡1(modp)a^2 \equiv 1 \pmod p if and only if a=1a = 1 or a=p−1a = p-1.

Proof

(⇐\Leftarrow) If a=1a = 1, then a2=1≡1(modp)a^2 = 1 \equiv 1 \pmod p. If a=p−1≡−1(modp)a = p - 1 \equiv -1 \pmod p, then a2≡(−1)2=1(modp)a^2 \equiv (-1)^2 = 1 \pmod p.

(⇒\Rightarrow) Suppose a2≡1(modp)a^2 \equiv 1 \pmod p, i.e. p∣a2−1=(a−1)(a+1)p \mid a^2 - 1 = (a-1)(a+1). Since pp is prime, pp is a prime element of Z\mathbb{Z} (Euclid's lemma: if a prime divides a product it divides one of the factors). Hence

p∣(a−1)orp∣(a+1).p \mid (a-1) \quad \text{or} \quad p \mid (a+1).

If p∣a−1p \mid a - 1, then since 1≤a≤p−11 \le a \le p-1 we have 0≤a−1≤p−20 \le a - 1 \le p - 2, and the only multiple of pp in this range is 00, forcing a−1=0a - 1 = 0, i.e. a=1a = 1. If p∣a+1p \mid a+1, then 2≤a+1≤p2 \le a+1 \le p, and the only multiple of pp in this range is pp itself, forcing a+1=pa + 1 = p, i.e. a=p−1a = p - 1. ■\blacksquare

∎
Proof

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: pp prime   ⟹  (p−1)!≡−1(modp)\implies (p-1)! \equiv -1 \pmod p. Base cases. For p=2p = 2: (p−1)!=1!=1≡−1(mod2)(p-1)! = 1! = 1 \equiv -1 \pmod 2 since −1≡1(mod2)-1 \equiv 1 \pmod 2. For p=3p = 3: (p−1)!=2!=2≡−1(mod3)(p-1)! = 2! = 2 \equiv -1 \pmod 3. Both check directly; assume now p≥5p \ge 5 (the argument below in fact also covers p=2,3p=2,3 but the pairing is vacuous/degenerate there, so we isolate them for clarity). Setup. Since pp is prime, every a∈{1,2,…,p−1}a \in \{1, 2, \dots, p-1\} is coprime to pp (as pp has no divisors other than 11 and pp, and a<pa < p). By the theory of Modular Inverses, for each such aa there is a unique a′∈{1,…,p−1}a' \in \{1, \dots, p-1\} with

a⋅a′≡1(modp).a \cdot a' \equiv 1 \pmod p.

(Existence and uniqueness of a′a' follow from Bézout's identity: since gcd⁡(a,p)=1\gcd(a,p)=1, there exist integers x,yx,y with ax+py=1ax + py = 1, so ax≡1(modp)a x \equiv 1 \pmod p; reducing xx modulo pp into the range {1,…,p−1}\{1,\dots,p-1\} gives a′a', and uniqueness holds because if aa′≡aa′′≡1a a' \equiv a a'' \equiv 1 then a′≡a′(aa′′)≡(a′a)a′′≡a′′(modp)a'\equiv a' (a a'') \equiv (a'a) a'' \equiv a''\pmod p, and both lie in {1,…,p−1}\{1,\dots,p-1\} so a′=a′′a'=a''.) Fixed points. By the Lemma above, a=a′a = a' (i.e., aa is its own inverse) exactly when a=1a = 1 or a=p−1a = p-1. Pairing off the rest. Consider the set S={2,3,…,p−2}S = \{2, 3, \dots, p-2\}, which has p−3p - 3 elements (an even number, since pp is odd for p≥3p \ge 3; indeed p−3p-3 is even iff pp is odd, true for all odd primes). The map a↦a′a \mapsto a' is an involution on SS: it sends SS into {1,…,p−1}\{1,\dots,p-1\}, and a′≠aa' \ne a for a∈Sa \in S (since a∉{1,p−1}a \notin \{1, p-1\}), and a′≠1,p−1a' \ne 1, p-1 either — because if a′=1a' = 1 then a⋅1≡1a \cdot 1 \equiv 1 forces a=1∉Sa = 1 \notin S, and if a′=p−1a' = p-1 then a≡(p−1)−1≡(−1)−1≡−1≡p−1(modp)a \equiv (p-1)^{-1} \equiv (-1)^{-1} \equiv -1 \equiv p - 1 \pmod p, forcing a=p−1∉Sa = p-1 \notin S. So the involution maps SS to itself, has no fixed points (since a=a′a=a' only at 1,p−11, p-1, both outside SS), and is its own inverse (applying it twice returns aa, since (a′)′=a(a')' = a). An involution without fixed points on a finite set partitions that set into disjoint 22-element orbits {a,a′}\{a, a'\}; hence SS splits into p−32\frac{p-3}{2} pairs, each satisfying a⋅a′≡1(modp)a \cdot a' \equiv 1 \pmod p. Assembling the product. Now compute:

(p−1)!=1⋅(∏a∈Sa)⋅(p−1)=1⋅(∏pairs {a,a′}a⋅a′)⋅(p−1).(p-1)! = 1 \cdot \Big(\prod_{a \in S} a\Big) \cdot (p-1) = 1 \cdot \Big(\prod_{\text{pairs } \{a,a'\}} a \cdot a'\Big) \cdot (p-1).

Each of the p−32\frac{p-3}{2} pair-products is ≡1(modp)\equiv 1 \pmod p, so the middle product is ≡1(p−3)/2=1(modp)\equiv 1^{(p-3)/2} = 1 \pmod p. Therefore

(p−1)!≡1⋅1⋅(p−1)≡p−1≡−1(modp).(p-1)! \equiv 1 \cdot 1 \cdot (p-1) \equiv p - 1 \equiv -1 \pmod p.

This proves the forward direction for all primes pp.

Direction 2: (n−1)!≡−1(modn)  ⟹  n(n-1)! \equiv -1 \pmod n \implies n prime (for n≥2n \ge 2). We prove the contrapositive: if n≥2n \ge 2 is composite, then (n−1)!≢−1(modn)(n-1)! \not\equiv -1 \pmod n; in fact we show (n−1)!≡0(modn)(n-1)! \equiv 0 \pmod n except in one explicit small case, and in all cases (n−1)!≢−1(n-1)! \not\equiv -1. Case A: n=4n = 4. Direct check: (n−1)!=3!=6≡2(mod4)(n-1)! = 3! = 6 \equiv 2 \pmod 4, and 2≢−1≡3(mod4)2 \not\equiv -1 \equiv 3\pmod 4. So the congruence fails for n=4n=4. Case B: nn composite, n≠4n \ne 4, so n≥6n \ge 6 or nn has a nontrivial factorization we can exploit; we split further. Since nn is composite, write n=abn = ab with 1<a≤b<n1 < a \le b < n (take aa to be the smallest divisor of nn greater than 11, so aa is prime and b=n/ab = n/a). Sub-case B1: a<ba < b. Then aa and bb are two distinct integers, both lying in {1,2,…,n−1}\{1, 2, \dots, n-1\} (since 1<a<b<n1 < a < b < n), so both appear as separate factors in the product (n−1)!=1⋅2⋯(n−1)(n-1)! = 1 \cdot 2 \cdots (n-1). Hence ab=nab = n divides (n−1)!(n-1)!, giving

(n−1)!≡0(modn).(n-1)! \equiv 0 \pmod n.

Sub-case B2: a=ba = b, i.e. n=a2n = a^2 with aa prime. If a≥3a \ge 3, then since n=a2≥9>2an = a^2 \ge 9 > 2a (as a≥3⇒a2−2a=a(a−2)≥3>0a \ge 3 \Rightarrow a^2 - 2a = a(a-2) \ge 3 > 0), both aa and 2a2a are distinct integers lying in {1,…,n−1}\{1, \dots, n-1\} (indeed 2a<a2=n2a < a^2 = n exactly when a>2a>2). So (n−1)!(n-1)! contains both aa and 2a2a as distinct factors, hence contains a⋅2a=2a2=2na \cdot 2a = 2a^2 = 2n as a divisor of the product, so certainly n∣(n−1)!n \mid (n-1)!, giving again (n−1)!≡0(modn)(n-1)! \equiv 0 \pmod n. If a=2a = 2, then n=4n = 4, which is exactly Case A, already handled separately (there 2⋅2a=8>n−1=32 \cdot 2a = 8 > n-1=3, so the argument of B2 for a≥3a\ge3 does not apply, which is precisely why n=4n=4 is the genuine exception). Conclusion of Direction 2. For every composite n≥2n \ge 2: if n=4n = 4, (n−1)!≡2≢−1(mod4)(n-1)! \equiv 2 \not\equiv -1 \pmod 4; if n≠4n \ne 4, (n−1)!≡0(modn)(n-1)! \equiv 0 \pmod n, and since n≥5n \ge 5 (the smallest composite other than 44) we have 0≢−1(modn)0 \not\equiv -1 \pmod n (as that would require n∣1n \mid 1, impossible for n≥2n \ge 2). In every composite case the Wilson congruence fails. Equivalently, if (n−1)!≡−1(modn)(n-1)! \equiv -1 \pmod n holds, nn cannot be composite; combined with the trivial fact that n=1n=1 is not prime and is excluded by hypothesis n≥2n\ge2, nn must be prime. Both directions are established, completing the proof of Wilson's Theorem in its full iff form. ■\blacksquare

∎

Worked examples

Example 1: Verifying Wilson's Theorem for p=13p = 13. We compute 12!  1312! \bmod 13 using the pairing method directly, rather than brute-force multiplying 12!=47900160012!= 479001600, to illustrate the proof's mechanism. We need inverses of 2,…,112, \dots, 11 modulo 1313 (excluding the fixed points 11 and 12≡−112 \equiv -1):

  • 2⋅7=14≡12 \cdot 7 = 14 \equiv 1, so 2′=72' = 7.
  • 3⋅9=27≡13 \cdot 9 = 27 \equiv 1 (since 27−26=127 - 26 = 1), so 3′=93' = 9.
  • 4⋅10=40≡14 \cdot 10 = 40 \equiv 1 (since 40−39=140 - 39 = 1), so 4′=104' = 10.
  • 5⋅8=40≡15 \cdot 8 = 40 \equiv 1, so 5′=85' = 8.
  • 6⋅11=66≡16 \cdot 11 = 66 \equiv 1 (since 66=5⋅13+166 = 5\cdot13+1), so 6′=116' = 11.

This accounts for all of {2,3,4,5,6,7,8,9,10,11}\{2,3,4,5,6,7,8,9,10,11\} in five pairs: (2,7),(3,9),(4,10),(5,8),(6,11)(2,7), (3,9), (4,10), (5,8), (6,11) — indeed p−32=102=5\frac{p-3}{2} = \frac{10}{2}=5 pairs as the theorem predicts. Each pair multiplies to 1(mod13)1 \pmod{13}, so

12!≡1⋅(2⋅7)(3⋅9)(4⋅10)(5⋅8)(6⋅11)⋅12≡1⋅1⋅1⋅1⋅1⋅1⋅12≡12≡−1(mod13).12! \equiv 1 \cdot (2\cdot7)(3\cdot9)(4\cdot10)(5\cdot8)(6\cdot11) \cdot 12 \equiv 1 \cdot 1\cdot1\cdot1\cdot1\cdot1 \cdot 12 \equiv 12 \equiv -1 \pmod{13}.

As a sanity check against direct computation: 12!=47900160012! = 479001600. Dividing, 479001600=13×36846276+12479001600 = 13 \times 36846276 + 12, since 13×36846276=47900158813 \times 36846276 = 479001588 and 479001600−479001588=12479001600 - 479001588 = 12. Indeed 12!≡12≡−1(mod13)12! \equiv 12 \equiv -1 \pmod{13}, confirming the pairing computation exactly.

Example 2: Testing the converse on a composite number, n=15n = 15, and locating exactly where it fails. Here n=15=3⋅5n = 15 = 3 \cdot 5, with a=3,b=5a = 3, b = 5 distinct and both less than 14=n−114 = n - 1. Sub-case B1 of the proof applies directly: both 33 and 55 occur as separate factors among 1,2,…,141, 2, \dots, 14 in the product 14!14!, so

15=3⋅5∣14!.15 = 3 \cdot 5 \mid 14!.

Concretely, 14!=1⋅2⋅3⋯1414! = 1\cdot2\cdot3\cdots14 contains the factor 33 (from the term "3") and the factor 55 (from the term "5") as genuinely separate multiplicands, so 3⋅5=153 \cdot 5 = 15 divides the product; hence 14!≡0(mod15)14! \equiv 0 \pmod{15}. We can double check with a partial direct reduction. Modulo 1515: 14!=1⋅2⋅3⋅4⋅5⋅6⋯1414! = 1\cdot2\cdot3\cdot4\cdot5\cdot6\cdots14. Already 3⋅4⋅5=60≡0(mod15)3 \cdot 4 \cdot 5 = 60 \equiv 0 \pmod{15} (since 60=4×1560 = 4 \times 15), so as soon as we multiply through the term 55, the running product is 0(mod15)0 \pmod{15}, and multiplying by anything more keeps it 00. Hence

14!≡0(mod15),0≢−1≡14(mod15).14! \equiv 0 \pmod{15}, \qquad 0 \not\equiv -1 \equiv 14 \pmod{15}.

So n=15n=15 decisively fails Wilson's congruence, exactly as the converse direction predicts for composite n≠4n \ne 4, and this failure is not a near-miss — the residue is 00, as far from −1-1 as a residue can be relative to divisibility structure, since 00 and −1≡14-1\equiv 14 differ by 1414, coprime-flavored evidence that no small perturbation fixes it.

Counterexamplewhy "prime" cannot be weakened to "nn has a prime factor" or the fixed-point count argument fails for composite nn

The forward direction of Wilson's Theorem crucially uses that every element of {1,…,n−1}\{1, \dots, n-1\} is invertible modulo nn — true only when nn is prime, since invertibility mod nn is equivalent to coprimality with nn. If we try to run the same pairing argument with nn composite, it breaks down at the very first step, and the theorem's conclusion genuinely fails. Take n=9n = 9 (composite, not prime). The residues 1,…,81, \dots, 8 are not all coprime to 99: specifically 33 and 66 share a factor of 33 with 99, so 33 and 66 have no multiplicative inverse modulo 99 at all. The pairing argument's foundational step — "every aa has a unique inverse a′a'" — is simply false for a=3a = 3 or a=6a=6 here; there is no xx with 3x≡1(mod9)3x \equiv 1 \pmod 9, because 3x(mod9)∈{0,3,6}3x \pmod 9 \in \{0,3,6\} for every xx, never 11. Consequently the entire cancellation-to-(−1)(-1) mechanism collapses. Concretely, 8!=403208! = 40320. Since 8!8! contains both factors 33 and 66 (and also 99's prime factor 33 appears again via the factor "6=2⋅36=2\cdot3"), we get 32∣8!3^2 \mid 8! and in fact 9∣8!9 \mid 8! directly (as 3⋅6=18=2⋅93\cdot 6 = 18 = 2\cdot 9). Explicitly, 40320/9=448040320 / 9 = 4480 exactly, so

8!≡0(mod9),not −1≡8(mod9).8! \equiv 0 \pmod 9, \qquad \text{not } {-1} \equiv 8 \pmod 9.

This shows precisely why primality is not just a convenient hypothesis but the load-bearing one: drop it, and the group-of-units structure (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times no longer includes all of {1,…,n−1}\{1,\dots,n-1\}, some factors become zero-divisors, the product is forced toward 00, and the clean pairing-to-(−1)(-1) argument has no analogue to fall back on.

Scope, edge cases & common traps

CaseWhat happensWhy
n=1n = 1Congruence holds trivially (everything is ≡0(mod1)\equiv 0 \pmod 1)Degenerate modulus; usually excluded from the "iff prime" statement by convention
n=2n = 2(2−1)!=1≡−1(mod2)(2-1)! = 1 \equiv -1 \pmod 2Smallest prime; pairing argument is vacuous (S=∅S=\varnothing) but base case checks directly
n=4n = 43!=6≡2(mod4)3! = 6 \equiv 2 \pmod 4, fails Wilson but is the unique composite exception to (n−1)!≡0(n-1)!\equiv 0n=4=22n=4=2^2 is too small for the two-distinct-multiples trick (needs 2a<n2a<n with a≥3a\ge3); direct check needed
nn composite, n≠4n \ne 4(n−1)!≡0(modn)(n-1)! \equiv 0 \pmod n, never ≡−1\equiv -1Two distinct proper factors of nn (or a repeated small prime with room to spare) both occur inside (n−1)!(n-1)!
nn prime, large(p−1)!(p-1)! is astronomically large; congruence still holds but is computationally useless as a primality testFactorial growth makes this test worse than trial division for all practical pp

The mathematician behind it

Origins · 1 minJohn Wilson and how this was discovered
JW
John Wilson
1741–1793 · English mathematician and judge

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.

History
- 1770: Edward Waring publishes the statement in Meditationes Algebraicae, attributing the observation to his former student John Wilson, and remarks that a proof seems difficult since there is no known notation for primes.
- 1771: Joseph-Louis Lagrange gives the first published proof of both the forward implication and its converse, establishing the full "iff" characterization of primes.
- 1773: Leonhard Euler, working independently and using properties of primitive roots, gives another proof and extends related congruence considerations.
- 1801: Carl Friedrich Gauss, in Disquisitiones Arithmeticae, generalizes Wilson's theorem to a statement about the product of all units modulo an arbitrary integer nn, expressing the product as −1-1 when nn has a primitive root and as +1+1 otherwise (apart from small exceptions), tying the phenomenon directly into the theory of Primitive Roots.
- 19th–20th centuries: Wilson's theorem becomes a standard textbook centerpiece (Hardy & Wright, Ireland & Rosen) illustrating group-theoretic pairing arguments in elementary number theory, and motivates later refinements such as Wilson primes (primes pp for which (p−1)!≡−1(modp2)(p-1)! \equiv -1 \pmod{p^2}).
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 (n−1)!  n(n-1)! \bmod n requires on the order of nn modular multiplications if done naively, which is drastically slower than trial division (about n\sqrt n divisions) or modern probabilistic tests like Miller–Rabin (polynomial in log⁡n\log n). Wilson's theorem is prized for its elegance and its role in illustrating the structure of (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times, 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 pp to be prime rather than just "coprime to something"?

Primality of pp guarantees that every nonzero residue 1,…,p−11, \dots, p-1 is invertible modulo pp, because coprimality with pp holds automatically for all integers strictly between 00 and pp precisely when pp has no divisors besides 11 and itself. This universal invertibility is what allows the pairing argument to partition the entire set {2,…,p−2}\{2, \dots, p-2\} into inverse pairs with no leftover elements. If pp were composite, some residues would be zero-divisors with no inverse, the pairing would be incomplete, and — as the counterexample with n=9n=9 shows — the product collapses toward 00 rather than −1-1.

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 nn (or an explicit repeated-factor argument) that force (n−1)!(n-1)! to be divisible by nn, hence congruent to 00, never −1-1 (except the isolated case n=4n=4, 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 pp satisfying the stronger congruence (p−1)!≡−1(modp2)(p-1)! \equiv -1 \pmod{p^2}, i.e., the Wilson quotient W(p)=((p−1)!+1)/pW(p) = \big((p-1)! + 1\big)/p is itself divisible by pp. Only three are currently known: p=5,13,563p = 5, 13, 563, 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 (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times for prime pp, but from different angles. Fermat's Little Theorem says every nonzero residue aa satisfies ap−1≡1(modp)a^{p-1} \equiv 1 \pmod p, a statement about the order of individual elements dividing p−1p-1. Wilson's Theorem instead computes the product of all elements at once, revealing it equals −1-1. They are logically independent as stated (neither proof requires the other here), though both ultimately reflect the same underlying fact: (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times is a group of order p−1p-1, 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 n≥2n \ge 2, consider the product P(n)P(n) of all elements of (Z/nZ)×(\mathbb{Z}/n\mathbb{Z})^\times (the residues coprime to nn, not all residues). Gauss showed P(n)≡−1(modn)P(n) \equiv -1 \pmod n if nn has a primitive root (i.e., n∈{1,2,4,pk,2pk}n \in \{1,2,4,p^k,2p^k\} for odd prime pp), and P(n)≡1(modn)P(n) \equiv 1 \pmod n otherwise. This recovers Wilson's Theorem exactly when n=pn=p is prime (where every residue 1,…,p−11,\dots,p-1 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 pp?

The pairing argument only tracks residues modulo pp itself; it shows each inverse pair multiplies to something congruent to 1(modp)1 \pmod p, but says nothing about how far from exactly 11 that product is (i.e., its value mod p2p^2 or higher). Determining (p−1)!  p2(p-1)! \bmod p^2 (the Wilson quotient) requires finer tools — such as properties of the pp-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.

Before this