This statement means two things, which are as follows:

(1) \quad \text{(1)} (1) For a positive integer n ( > 1 ) n\ (> 1) n (>1), if ( n − 1 ) ! ≡ − 1 ( m o d n ) , (n-1)!\equiv -1\pmod n, (n−1)!≡−1(modn), then n n n is a prime.

(2) \quad \text{(2)} (2) If p p p is a prime number, then ( p − 1 ) ! ≡ − 1 ( m o d p ) (p-1)!\equiv -1\pmod p (p−1)!≡−1(modp) holds.

Notice how ( 1 ) (1) (1) provides us with a way to check if a number is prime. But this is really inefficient as factorials grow really fast. So it's hard to compute the modulus for sufficiently large n n n even on a computer. Fortunately, we've got better primality tests to save the world! But ( 2 ) (2) (2) is helpful in easing out computations and cracking several Olympiad number theory problems.

Let's verify Wilson's theorem for small values:

n ( n − 1 ) ! ( n − 1 ) ! ( m o d n ) Is prime? 2 1 1 yes 3 2 2 yes 4 6 2 no 5 24 4 yes 6 120 0 no 7 720 6 yes 8 5040 0 no 9 40320 0 no 10 362880 0 no \begin{array} {| l | r | c | r| } \hline n & (n-1)! & (n-1)! \pmod{n} & \text{ Is prime?} \\ \hline 2 & 1 & 1 & \text{yes} \\ 3 & 2 & 2 & \text{yes} \\ 4 & 6 & 2 & \text{no} \\ 5 & 24 & 4 & \text{yes} \\ 6 & 120 & 0 & \text{no} \\ 7 & 720 & 6 & \text{yes} \\ 8 & 5040 & 0 & \text{no} \\ 9 & 40320 & 0 & \text{no} \\ 10 & 362880 & 0 & \text{no} \\ \hline \end{array} n2345678910​(n−1)!12624120720504040320362880​(n−1)!(modn)122406000​ Is prime?yesyesnoyesnoyesnonono​​

Evaluate 30 ! ( m o d 899 ) 30! \pmod{899} 3 0 ! ( m o d 8 9 9 ) . First, factorize 899 = 29 × 31 899 = 29 \times 31 899=29×31.

We have 30 ! = 30 × 29 × 28 ! ≡ 0 ( m o d 29 ) 30 ! = 30 \times 29 \times 28 ! \equiv 0 \pmod{29} 30!=30×29×28!≡0(mod29).

From Wilson's theorem, 30 ! ≡ − 1 ( m o d 31 ) 30 ! \equiv -1 \pmod{31} 30!≡−1(mod31).

Hence, by the Chinese remainder theorem, we get that 30 ! ≡ 464 ( m o d 899 ) 30! \equiv 464 \pmod{ 899} 30!≡464(mod899). □ _\square □​



Explain why 17 ! ≡ 1 ( m o d 19 ) 17 ! \equiv 1 \pmod{ 19} 1 7 ! ≡ 1 ( m o d 1 9 ) . From Wilson's theorem, we know that 18 ! ≡ − 1 ( m o d 19 ) 18 ! \equiv -1 \pmod{19} 18!≡−1(mod19). Hence, dividing both sides by 18 ≡ − 1 ( m o d 19 ) 18 \equiv -1 \pmod{19} 18≡−1(mod19), we conclude that 17 ! ≡ 1 ( m o d 19 ) 17! \equiv 1 \pmod{19} 17!≡1(mod19). □ _\square □​

In fact, the above example generalizes to the following:

If p p p is a prime, then ( p − 2 ) ! ≡ 1 ( m o d p ) (p-2) ! \equiv 1 \pmod{p} (p−2)!≡1(modp). □ _\square □​

If p p p is a prime, then by Wilson's theorem, we know that ( p − 1 ) ! ≡ − 1 ≡ p − 1 ( m o d p ) ( p - 1)! \equiv -1\equiv p-1 \pmod{p} (p−1)!≡−1≡p−1(modp). Dividing by p − 1 ≠ 0 p - 1

eq 0 p−1​=0, we get that ( p − 2 ) ! ≡ 1 ( m o d p ) . ( p -2)! \equiv 1 \pmod{p} . (p−2)!≡1(modp). Hence, ( p − 2 ) ! ≡ 1 ( m o d p ) for all primes p . □ (p-2)!\equiv 1\pmod{p}~\text{for all primes }p. \ _\square (p−2)!≡1(modp) for all primes p. □​