Wilson's theorem using factorization of xp−x over Fp
Recall that
xp−x≡x(x−1)(x−2)⋯(x−(p−1))(modp).
Thus, we have
xp−1−1≡(x−1)(x−2)⋯(x−(p−1))(modp).
This can be derived from Fermat's Little Theorem (∀x∈{1,2,⋯,p−1},xp−1≡1(modp)). The result follows after using the factor theorem p−1 times.
Let x=p. Then, we have
pp−1−1≡(p−1)(p−2)⋯(p−(p−1))(modp).
0−1≡(p−1)(p−2)⋯2⋅1(modp).
−1=(p−1)!(modp)
This result is called Wilson's theorem. Actually, it's only one direction. Wilson's theorem actually states that if this is true, then p must be prime as well. Let's prove that direction.
We wish to prove that if −1=(p−1)!(modp), then p is prime. Suppose for the sake of contradiction, that this is true, and p is not prime. Then, we know p=ab for some 1<a,b<p where gcd(a,b)=1. However, this tells us that a and b are in (p−1)!. That means that p=ab∣(p−1)!, so (p−1)!≡0(modp), which is a contradiction. Thus, p must be prime.