Wilson's Theorem

Theorem
A (strictly) positive integer $p$ is a prime :
 * $\paren {p - 1}! \equiv -1 \pmod p$

Proof
If $p = 2$ the result is obvious.

Therefore we assume that $p$ is an odd prime.

Necessary Condition
Let $p$ be prime.

Consider $n \in \Z, 1 \le n < p$.

As $p$ is prime, $n \perp p$.

From Law of Inverses (Modulo Arithmetic), we have:


 * $\exists n' \in \Z, 1 \le n' < p: n n' \equiv 1 \pmod p$

By Solution of Linear Congruence, for each $n$ there is exactly one such $n'$, and $\paren {n'}' = n$.

So, provided $n \ne n'$, we can pair any given $n$ from $1$ to $p$ with another $n'$ from $1$ to $p$.

We are then left with the numbers such that $n = n'$.

Then we have $n^2 \equiv 1 \pmod p$.

Consider $n^2 - 1 = \paren {n + 1} \paren {n - 1}$ from Difference of Two Squares.

So either $n + 1 \divides p$ or $n - 1 \divides p$.

Observe that these cases do not occur simultaneously, as their difference is $2$, and $p$ is an odd prime.

From Congruence Modulo Negative Number‎, we have that $p - 1 \equiv -1 \pmod p$.

Hence $n = 1$ or $n = p - 1$.

So, we have that $\paren {p - 1}!$ consists of numbers multiplied together as follows:


 * in pairs whose product is congruent to $1 \pmod p$
 * the numbers $1$ and $p - 1$.

The product of all these numbers is therefore congruent to $1 \times 1 \times \cdots \times 1 \times p - 1 \pmod p$ by modulo multiplication.

From Congruence Modulo Negative Number we therefore have that $\paren {p - 1}! \equiv -1 \pmod p$.

Sufficient Condition
Now assume $p$ is composite, and $q$ is a prime such that $q \divides p$.

Then both $p$ and $\paren {p - 1}!$ are divisible by $q$.

If the congruence $\paren {p - 1}! \equiv -1 \pmod p$ were satisfied, we would have $\paren {p - 1}! \equiv -1 \pmod q$.

However, this amounts to $0 \equiv -1 \pmod q$, a contradiction.

Hence for $p$ composite, the congruence $\paren {p - 1}! \equiv -1 \pmod p$ cannot hold.

Also known as
Some sources refer to this theorem as the Wilson-Lagrange theorem, after, who proved it.