Maths WorkshopMaths problems with full solutions
Exercise 1590Number theory › CongruencesOlympiadAdded 3 Nov 2026

Wilson's theorem

#Wilson theorem#prime#factorial

Statement

Show that pp is prime iff (p−1)!≡−1(modp)(p-1)!\equiv-1\pmod p.

Hint

If pp is prime, pair each kk with its inverse mod pp; only ±1\pm1 are self-inverse.

Show solutionHide solution

Solution

pp prime ⇒(p−1)!≡−1\Rightarrow(p-1)!\equiv-1. Among 1,…,p−11,\ldots,p-1, pair kk with k−1k^{-1}. The only self-inverse elements satisfy k2≡1k^2\equiv1, i.e. k≡±1k\equiv\pm1. All other pairs multiply to 11, so (p−1)!≡1⋅(−1)⋅1⋯1≡−1(modp)(p-1)!\equiv1\cdot(-1)\cdot1\cdots1\equiv-1\pmod p.

Converse. If nn composite and n>4n>4: nn has a proper divisor dd with 1<d<n1<d<n, so d∣(n−1)!d\mid(n-1)!, and d∣nd\mid n; thus n∤(n−1)!+1n\nmid(n-1)!+1, so (n−1)!≢−1(modn)(n-1)!\not\equiv-1\pmod n. (Small cases: n=4n=4, (3)!=6≡2≠−1(3)!=6\equiv2\neq-1.)

Conclusion. Wilson's criterion holds.

Application. p=5p=5: 4!=24≡−1(mod5)4!=24\equiv-1\pmod5. Correct.