Number TheoryFermat, Euler and Wilson

Wilson's Theorem

An integer p > 1 is prime exactly when (p − 1)! ≡ −1 (mod p): the factors from 2 to p − 2 pair up with their inverses, leaving 1 · (p − 1) ≡ −1.

Not started Jump to practice

Multiply all the nonzero residues mod a prime. Mod 5: $1 \cdot 2 \cdot 3 \cdot 4 = 24 \equiv -1$. Mod 7: $6! = 720 \equiv -1$. It is always $-1$, and the reason is a pretty pairing argument with modular inverses.

Wilson's theorem. An integer $p > 1$ is prime exactly when $$(p - 1)! \equiv -1 \pmod{p}.$$

Why it works

Primes. Mod a prime $p$, every $x$ from $1$ to $p - 1$ has an inverse. Which are their own inverses? $x^{2} \equiv 1$ means $p \mid (x - 1)(x + 1)$, so by Euclid's lemma $x \equiv 1$ or $x \equiv -1$. Every other number from $2$ to $p - 2$ pairs off with a different partner, and each pair multiplies to $1$. So $(p - 1)! \equiv 1 \cdot (p - 1) \equiv -1$.
Composites. If $n = ab$ with $1 < a < b < n$, both $a$ and $b$ appear in $(n - 1)!$, so $n \mid (n - 1)!$. If $n = a^{2}$ with $a > 2$, then $a$ and $2a$ both appear, so again $n \mid (n - 1)!$. Either way $(n - 1)! \equiv 0$, not $-1$. The lone exception $n = 4$ gives $3! = 6 \equiv 2$, still not $-1$.
Mod 13, each chord joins a residue to its inverse: 2 and 7, 3 and 9, 4 and 10, 5 and 8, 6 and 11. Only 1 and 12 are left alone, so $12! \equiv 1 \cdot 12 \equiv -1$. Try $n = 12$: most points have no partner at all.

Worked example: Checking the theorem

Find $12! \bmod 13$ without computing $12!$.
  1. Pair each number with its inverse mod 13: $2 \cdot 7 = 14$, $3 \cdot 9 = 27$, $4 \cdot 10 = 40$, $5 \cdot 8 = 40$, $6 \cdot 11 = 66$ are all $\equiv 1$.
  2. $12! \equiv 1 \cdot 12 \cdot 1^{5} = 12 \equiv -1 \pmod{13}$.

Answer: $12$, that is, $-1$.

Worked example: One factor short

Find $15! \bmod 17$.
  1. Wilson: $16! \equiv -1 \pmod{17}$. And $16! = 15! \cdot 16$ with $16 \equiv -1$.
  2. So $15! \cdot (-1) \equiv -1$, which gives $15! \equiv 1 \pmod{17}$.

Answer: $1$.

Worked example: Two factors short

Find $20! \bmod 23$.
  1. $22! = 20! \cdot 21 \cdot 22 \equiv 20! \cdot (-2)(-1) = 2 \cdot 20! \pmod{23}$, and $22! \equiv -1$.
  2. So $2 \cdot 20! \equiv -1$. The inverse of 2 mod 23 is 12, so $20! \equiv -12 \equiv 11 \pmod{23}$.

Answer: $11$.

For primes the value is $n - 1 \equiv -1$; for composites it is $0$, except $2$ at $n = 4$.
$n$234567891011
$(n-1)! \bmod n$12240600010

Common mistake

Expecting $(n - 1)! \equiv -1 \pmod{n}$ for every $n$. For $n = 6$: $5! = 120 = 6 \cdot 20 \equiv 0$. The theorem is about primes, and for composites the value is $0$ (or $2$ when $n = 4$). That difference is exactly why Wilson's theorem characterizes primes.
As a primality test Wilson's theorem is perfect and useless: it never errs, but computing $(n - 1)! \bmod n$ takes about $n$ multiplications, far slower than trial division. Its value is the pairing idea, which comes back in quadratic residues.

Key idea

$p$ is prime exactly when $(p - 1)! \equiv -1 \pmod{p}$: the residues $2, \dots, p - 2$ pair up with their inverses, leaving $1 \cdot (p - 1)$.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 2 XP.

Loading practice…

Builds on

Leads to

This is one of the top rungs of Number Theory.

Sources and further reading

  • William Stein, Elementary Number Theory: Primes, Congruences, and Secrets (Springer, 2009; free online edition), chapter 2, The Ring of Integers Modulo n (Wilson's theorem)
  • MIT OpenCourseWare 18.781 Theory of Numbers (Spring 2012), lecture notes on Wilson's theorem
@teddyrookbook GitHub