next up previous
Next: The Chinese Remainder Theorem Up: Lecture 6: Congruences, Part Previous: Lecture 6: Congruences, Part

Wilson's Theorem

Theorem 1.1 (John Wilson's theorem, from the 1770s)   An integer $ p>1$ is prime if and only if

$\displaystyle (p-1)! \equiv -1 \pmod{p}.$

Example 1.2  
? p=3
%1 = 3
? (p-1)! % 3
%2 = 2
? p=17
%3 = 17
? (p-1)!
%4 = 20922789888000
? (p-1)! % p
%5 = 16

Proof. We first assume that $ p$ is prime and prove that $ (p-1)! \equiv -1\pmod{p}$. If $ a\in\{1,2,\ldots,p-1\}$ then the equation

$\displaystyle ax\equiv 1\pmod{p}
$

has a unique solution $ a'\in\{1,2,\ldots,p-1\}$. If $ a=a'$, then $ a^2\equiv 1\pmod{p}$, so $ p\mid a^2-1 = (a-1)(a+1)$, so $ p\mid (a-1)$ or $ p\mid (a+1)$, so $ a\in\{1,-1\}$. We can thus pair off the elements of $ \{2,3,\ldots,p-2\}$, each with its inverse. Thus

$\displaystyle 2\cdot 3 \cdot \cdots \cdot (p-2) \equiv 1\pmod{p}.
$

Multiplying both sides by $ p-1$ proves that $ (p-1)! \equiv -1\pmod{p}$.

Next we assume that $ (p-1)! \equiv -1\pmod{p}$ and prove that $ p$ must be prime. Suppose not, so that $ p$ is a composite number $ \geq 4$. Let $ \ell$ be a prime divisor of $ p$. Then $ \ell<p$, so $ \ell\mid (p-1)!$. Also,

$\displaystyle \ell \mid p \mid ((p-1)! - 1).
$

This is a contradiction, because a prime can't divide a number $ a$ and also divide $ a-1$, since it would then have to divide $ a-(a-1)=1$. $ \qedsymbol$

Example 1.3   When $ p=17$, we have

$\displaystyle 2\cdot 3\cdot \cdots \cdot 15
= (2\cdot 9)\cdot(3\cdot 6)\cdot(4\...
...(5\cdot 7)\cdot(8\cdot 15)\cdot(10\cdot 12)\cdot(14\cdot 11)
\equiv 1\pmod{17},$

where we have paired up the numbers $ a, b$ for which $ ab\equiv 1\pmod{17}$.

Let's test Wilson's Theorem in PARI:

? wilson(n) = Mod((n-1)!,n) == Mod(-1,n)   
? wilson(5)
%9 = 1
? wilson(10)
%10 = 0
? wilson(389)
%11 = 1
? wilson(2001)
%12 = 0



Warning: In practice, this is a horribly inefficient way to check whether or not a number is prime.


next up previous
Next: The Chinese Remainder Theorem Up: Lecture 6: Congruences, Part Previous: Lecture 6: Congruences, Part
William A Stein 2001-09-23