Maths WorkshopMaths problems with full solutions
Exercise 1282Number theory › PrimesOlympiadAdded 4 Nov 2026

Wilson's theorem and its converse

#Wilson's theorem#modular inverses#primality

Statement

  1. Prove that for a prime pp, (p−1)!≡−1(modp)(p-1)!\equiv-1\pmod p.
  2. Prove that an integer n>1n>1 is prime if and only if n∣(n−1)!+1n\mid(n-1)!+1.
Hint

In {2,…,p−2}\{2,\dots,p-2\} pair each number with its inverse modulo pp. Which numbers are their own inverse?

Show solutionHide solution

Solution

1. For p=2,3p=2,3 the claim is checked directly (1!=1≡−1(mod2)1!=1\equiv-1\pmod2, 2!=2≡−1(mod3)2!=2\equiv-1\pmod3). Let p≥5p\ge5. Each a∈{1,…,p−1}a\in\{1,\dots,p-1\} has a unique inverse a−1a^{-1} in {1,…,p−1}\{1,\dots,p-1\} modulo pp. We have a=a−1a=a^{-1} iff a2≡1a^2\equiv1 iff p∣(a−1)(a+1)p\mid(a-1)(a+1) iff a≡±1a\equiv\pm1, i.e. a∈{1,p−1}a\in\{1,p-1\}.

Hence the p−3p-3 numbers 2,3,…,p−22,3,\dots,p-2 split into pairs {a,a−1}\{a,a^{-1}\} with product ≡1\equiv1, so 2⋅3⋯(p−2)≡12\cdot3\cdots(p-2)\equiv1, and
(p−1)!≡1⋅1⋅(p−1)≡−1(modp).(p-1)!\equiv1\cdot1\cdot(p-1)\equiv-1\pmod p.

2. (⇒\Rightarrow) is part 1. (⇐\Leftarrow) Suppose nn is composite, with a divisor dd such that 1<d<n1<d<n. Then d∣(n−1)!d\mid(n-1)!, so d∤(n−1)!+1d\nmid(n-1)!+1, and therefore n∤(n−1)!+1n\nmid(n-1)!+1. Hence n∣(n−1)!+1n\mid(n-1)!+1 forces nn prime.

Remark. Wilson's criterion is a perfect primality test in theory, but useless in practice because (n−1)!(n-1)! is huge.

Lesson. In a group of residues, pair every element with its inverse: only the elements with a2=1a^2=1 survive.