Section 5.6 Wilson's Theorem
Wilson's theorem is one of the few "if and only if" statements about primes.
Theorem 5.6.1 (Wilson, proved by Lagrange in 1771).
The integer \(n\) is prime if and only if \((n-1)! \equiv -1 \pmod{n}.\)
Proof.
\((\Rightarrow)\) For \(p = 2, 3\) the theorem is clear. Now suppose that \(p\gt 3,\) and that
Note that \(\gcd(p, a) = 1\) for such \(a.\) By a previous theorem the inverse of \(a\) is unique. Also if \(a\) is its own inverse, we know that \(a^2\equiv 1\pmod{p}\) so that
and so \(a = 1\) or \(a = p-1.\) Thus the set
is a set of even numbers such that if \(a\in A,\) then \(a^{-1}\in A,\) and \(a\neq a^{-1}.\) This gives
which is the same as
so that multiplying by \(p-1\) on both sides gives
\((\Leftarrow)\) Suppose \((n-1)!\equiv -1\pmod{n},\) and \(n\) is not prime. Then there is a divisor \(d\gt 1\) of \(n.\) Now if \(n\vert (n-1)! + 1,\) then \(d\vert (n-1)! + 1,\) so that since \(d\vert (n-1)!\) we have that \(d\vert 1,\) which is absurd. Thus \(n\) is prime.