Wilson's Theorem and Its Short Proof
Prove that (p − 1)! leaves remainder −1 exactly when p is prime, see the pairing trick in action and learn what the result is good for.
Key takeaways
- For a prime p, the product 1 × 2 × ... × (p − 1) is congruent to −1 modulo p.
- The converse also holds: if (n − 1)! ≡ −1 (mod n) with n > 1, then n is prime.
- The proof pairs each remainder with its multiplicative inverse, leaving only 1 and p − 1 unpaired.
- The theorem is a perfect primality criterion in logic but a useless one in practice, because factorials are so costly.
- It yields a square root of −1 modulo any prime that leaves remainder 1 on division by 4.
Wilson's theorem says that a whole number n greater than 1 is prime exactly when (n − 1)! leaves remainder n − 1, that is, −1, on division by n. The symbol (n − 1)! is the factorial, the product of all whole numbers from 1 to n − 1.
For n = 5, the product is 24, which is one less than 25. For n = 6, the product is 120, which is divisible by 6. The first is prime and the second is not. The proof takes a few lines and rests on one simple idea: numbers pair up with their inverses.
Checking small cases
| n | (n − 1)! | remainder mod n | prime? |
|---|---|---|---|
| 2 | 1 | 1, which is −1 | yes |
| 3 | 2 | 2 | yes |
| 4 | 6 | 2 | no |
| 5 | 24 | 4 | yes |
| 6 | 120 | 0 | no |
| 7 | 720 | 6 | yes |
| 8 | 5,040 | 0 | no |
| 9 | 40,320 | 0 | no |
For 7 you can verify directly: 720 = 103 × 7 − 1. If remainders are new to you, the introduction to modular arithmetic explains the notation ≡.
Inverses modulo a prime
Let p be prime. Every number a from 1 to p − 1 has an inverse: a number b in the same range with a × b ≡ 1 (mod p). This follows from Bezout's identity, because a and p are coprime.
The inverse is unique, and the inverse of b is a again. So the numbers from 1 to p − 1 fall into partnerships.
Can a number be its own partner? That requires a² ≡ 1, so p divides (a − 1)(a + 1). A prime dividing a product divides one of the factors. Hence a ≡ 1 or a ≡ −1. Only 1 and p − 1 stand alone.
The proof
Take the factorial and rearrange its factors so that partners sit together. Every pair multiplies to 1 and disappears. What remains is 1 × (p − 1), which is congruent to −1.
Here is the whole thing for p = 11.
10! = 1 × (2×6) × (3×4) × (5×9) × (7×8) × 10
12 12 45 56
≡ 1 × 1 × 1 × 1 × 1 × 10
≡ 10 ≡ −1 (mod 11) As a check, 10! = 3,628,800 and 3,628,801 = 11 × 329,891.
For p = 2 and p = 3 there are no pairs at all, and the claim can be verified directly: 1! = 1 ≡ −1 modulo 2, and 2! = 2 ≡ −1 modulo 3.
The converse
Suppose n > 1 and (n − 1)! ≡ −1 (mod n). Could n be composite? Then n has a divisor d with 1 < d < n. This d appears among the factors of (n − 1)!, so d divides the factorial. It also divides n, and therefore divides (n − 1)! + 1. So d divides the difference of those two numbers, which is 1. That is impossible.
Composite numbers behave even more decisively. For every composite n greater than 4, the factorial (n − 1)! is divisible by n. If n splits into two different factors a and b, both appear in the product. If n = p² with p an odd prime, then p and 2p both appear. Only n = 4 escapes, with 3! = 6 ≡ 2.
A second proof from Fermat
Fermat's little theorem says each of 1, 2, ..., p − 1 is a root of x^(p−1) − 1 modulo p. A polynomial of degree p − 1 over the integers modulo a prime with p − 1 known roots factors completely.
x^(p−1) − 1 ≡ (x − 1)(x − 2)···(x − (p−1))
set x = 0:
−1 ≡ (−1)^(p−1) × (p−1)! For odd p the sign (−1)^(p−1) is +1, and Wilson's theorem follows at once.
Reading the proof closely
Each step uses primality somewhere, and noticing where is a good way to understand the theorem.
The existence of inverses needs every number from 1 to p − 1 to be coprime to p. Modulo 12, the number 4 has no inverse at all, because 4x is always 0, 4 or 8.
The claim that only 1 and −1 are their own inverses also needs a prime. Modulo 8, all of 1, 3, 5 and 7 square to 1. The step "a prime dividing a product divides a factor" is exactly what fails: 8 divides 2 × 4 although it divides neither number.
Finally, the pairing itself needs partners to be unique, so that no number is claimed by two pairs. Uniqueness holds because if ab ≡ 1 and ac ≡ 1, then multiplying the second equation by b gives c ≡ b.
This habit of asking where a hypothesis is used pays off in any proof. It shows which conditions are essential and which might be relaxed, as in the generalization to composite moduli described further below.
A consequence for binomial coefficients
Wilson's theorem also simplifies expressions built from factorials. Take a prime p and a number k from 0 to p − 1. The factors of (p − 1)! above p − 1 − k can be rewritten as negatives of 1, 2, ..., k.
(p−1)! ≡ (p−1−k)! × (−1)^k × k!
so k! × (p−1−k)! ≡ (−1)^(k+1) Test it with p = 7 and k = 2: the left side is 2 × 24 = 48, which leaves remainder 6, that is −1, and the right side is (−1)³ = −1. An equivalent statement is that the binomial coefficient "p − 1 choose k" leaves remainder (−1)^k modulo p. In row 6 of Pascal's triangle, the entries 1, 6, 15, 20, 15, 6, 1 leave remainders alternating between 1 and 6 modulo 7.
Why it is not a practical test
Here is a criterion that is true exactly for primes, which sounds like the perfect test. The problem is cost. Computing (n − 1)! modulo n takes about n multiplications in the obvious method. For a number with 100 digits, that is far more steps than any computer could perform.
By comparison, tests based on powers need only a few hundred squarings for such a number. See how large numbers are tested. Faster ways to compute factorials modulo n exist, but they remain far slower than the usual tests.
A square root of minus one
One genuine use is theoretical. Let p be a prime of the form 4k + 1, and let h = (p − 1)/2. In the factorial, replace each number k above h with −(p − k).
(p−1)! ≡ h! × (−1)^h × h! Since h is even here, the sign is +1, and the left side is −1. So (h!)² ≡ −1.
p = 13, h = 6, 6! = 720 ≡ 5
5² = 25 ≡ −1 (mod 13) So −1 is a square modulo 13. This fact is a key step toward showing which primes are sums of two squares, and it fits into the theory of quadratic residues. For primes of the form 4k + 3, no square root of −1 exists.
History of the name
The result was known to the scholar Ibn al-Haytham around the year 1000. In Europe, Edward Waring published it in 1770, crediting his former student John Wilson, who had apparently noticed it without proving it. Joseph-Louis Lagrange gave the first published proof in 1771, including the converse.
As often in mathematics, the attached name records one route of transmission more than the full history.
Variations
The identity can be shifted. Dividing both sides by p − 1, which is −1, gives (p − 2)! ≡ 1 (mod p).
p = 7: 5! = 120 = 17 × 7 + 1
p = 11: 9! = 362,880 = 32,989 × 11 + 1 You can also ask when p² divides (p − 1)! + 1. Such primes are called Wilson primes. The known examples are 5, 13 and 563, and searches reaching far higher have found no others as of the mid-2020s. For 5, the value 4! + 1 = 25 is exactly 5².
Heuristically one expects infinitely many, but extremely thinly spread. Nobody has proved that there are infinitely many. The heuristic treats the quotient ((p − 1)! + 1)/p as if its remainder on division by p were random, so that it vanishes with chance about 1/p. The sum of 1/p over the primes grows without bound, but so slowly that long searches may easily find nothing new.
The pairing idea elsewhere
Pairing each element with its inverse is a technique worth keeping. In any finite commutative group, the product of all elements equals the product of those that are their own inverses, because everything else cancels in pairs.
For composite moduli this gives a generalization due to Gauss. Multiply all the numbers below n that are coprime to n. The result is −1 modulo n when n is 4, a power of an odd prime or twice such a power, and +1 for every other n of 3 or more.
n = 8: 1 × 3 × 5 × 7 = 105 ≡ 1
n = 9: 1 × 2 × 4 × 5 × 7 × 8 = 2240 ≡ 8 ≡ −1
n = 10: 1 × 3 × 7 × 9 = 189 ≡ 9 ≡ −1 The moduli with answer −1 are exactly those with primitive roots.
Common questions
Does the theorem give a formula for primes?
In a formal sense it can be turned into one. Since (n − 1)! + 1 is divisible by n only for primes, clever expressions using floors and trigonometric functions can be built that output primes. They merely hide the factorial test inside the notation, so they are far slower than a sieve and reveal nothing new about where primes lie.
What happens to n factorial itself?
For a prime p, the number p! is of course divisible by p. Wilson's theorem concerns the product that stops one step earlier, where no factor of p has yet appeared. That is exactly why the remainder is nonzero and informative.
Is there a version with other remainders?
Stopping earlier gives other identities, such as the one for (p − 2)! above. Stopping at half the range gives the square root of −1 when p is of the form 4k + 1. For primes of the form 4k + 3, the half factorial is itself congruent to 1 or −1, and which of the two occurs follows a much subtler pattern.
Try it
Work out the pairs for p = 13. You should find (2, 7), (3, 9), (4, 10), (5, 8) and (6, 11), with 1 and 12 left over. Multiply each pair and confirm that the remainder is 1. In five small calculations you will have verified that 12! + 1 is divisible by 13, without ever computing 12!.