Sub topic 12
Wilson's Theorem
This chapter assumes you can compute remainders of products and powers with congruence notation, and that you know Fermat's little theorem from the previous chapter. Wilson's theorem covers a narrower case. It gives the remainder of a factorial when the divisor is prime. In CAT, it usually shows up as "what is the remainder when (p − 1)! or a nearby factorial is divided by the prime p?"
The statement
For a prime p:
(p − 1)! ≡ −1 (mod p)
Equivalently, (p − 1)! leaves remainder p − 1 on division by p.
import math
print(math.factorial(12) % 13) # 12
print(math.factorial(6) % 7) # 6Both results are p − 1, as the theorem predicts. The theorem needs p to be prime. For a composite n, (n − 1)! is usually divisible by n, so the remainder is 0. The one exception is n = 4, where 3! = 6 leaves remainder 2.
import math
for n in [4, 6, 8, 9, 10]:
print(n, math.factorial(n - 1) % n)
# 4 2
# 6 0
# 8 0
# 9 0
# 10 0This gives a full characterisation. For n > 1, (n − 1)! ≡ −1 (mod n) holds exactly when n is prime. You will rarely need that "only if" direction in CAT. Its practical use is to rule out Wilson's theorem when the modulus is composite.
Why it works
You do not need to memorise a proof, but the idea explains how to use the theorem.
Take p = 7. The numbers 1, 2, ..., 6 are all invertible mod 7, because 7 is prime. Each number has exactly one partner in that range whose product with it is 1 (mod 7):
- 2 × 4 = 8 ≡ 1
- 3 × 5 = 15 ≡ 1
- 1 × 1 = 1 and 6 × 6 = 36 ≡ 1 pair with themselves
Only 1 and p − 1 are their own partners. So in 6! = 1 × 2 × 3 × 4 × 5 × 6, the pairs (2, 4) and (3, 5) each contribute 1. What remains is 1 × 6, which is ≡ −1 (mod 7).
Using Wilson's theorem directly
Many problems ask for the remainder of (p − 1)! directly.
Example 1. Find the remainder when 16! is divided by 17.
17 is prime, so 16! ≡ −1 ≡ 16 (mod 17). The remainder is 16.
Factorials one or two steps below p − 1
A common CAT variant asks for (p − 2)! or (p − 3)!. The trick is to write (p − 1)! in terms of the smaller factorial and use negative remainders.
(p − 1)! = (p − 1) × (p − 2)!
Since p − 1 ≡ −1 (mod p), this gives:
−1 ≡ (−1) × (p − 2)! (mod p)
So (p − 2)! ≡ 1 (mod p). Dividing by −1 is legal here because −1 is invertible mod p.
Example 2. Find the remainder when 15! is divided by 17.
From 16! = 16 × 15!, we have −1 ≡ (−1) × 15!, so 15! ≡ 1 (mod 17). The remainder is 1.
For (p − 3)!, use (p − 2)! = (p − 2) × (p − 3)!. Since (p − 2)! ≡ 1 and p − 2 ≡ −2:
1 ≡ (−2) × (p − 3)! (mod p)
You need an x with −2x ≡ 1 (mod p). For p = 17, −2x ≡ 1 means 2x ≡ −1 ≡ 16, so x ≡ 8.
Example 3. Find the remainder when 14! is divided by 17.
14! ≡ 8 (mod 17). Check: −2 × 8 = −16 ≡ 1 (mod 17). ✓
import math
print(math.factorial(16) % 17) # 16
print(math.factorial(15) % 17) # 1
print(math.factorial(14) % 17) # 8The general procedure for (p − k)! is to peel off one factor at a time, replace each factor by its negative remainder, and solve a small congruence for the unknown. This works for small k. For large k, a different method is usually faster.
Combining Wilson's theorem with other facts
Some questions include a factorial inside a larger expression. Look for terms that are zero mod p.
Example 4. Find the remainder when 13! + 14! + 15! is divided by 11.
Each of 13!, 14!, 15! contains the factor 11, so each is divisible by 11. The remainder is 0. No Wilson needed. This is the factorial property from the next chapter: n! ≡ 0 (mod p) whenever n ≥ p.
Example 5. Find the remainder when 10! + 9! is divided by 11.
By Wilson, 10! ≡ −1 (mod 11). Since 10! = 10 × 9! and 10 ≡ −1, we get 9! ≡ 1. So 10! + 9! ≡ −1 + 1 = 0. The remainder is 0.
Example 6. Find the remainder when 2 × 26! is divided by 29.
29 is prime, so 28! ≡ −1 (mod 29). Write 28! = 28 × 27 × 26!. Mod 29, 28 ≡ −1 and 27 ≡ −2, so 28! ≡ 2 × 26!. Therefore 2 × 26! ≡ −1 ≡ 28 (mod 29). The remainder is 28.
import math
print(math.factorial(10) % 11 , math.factorial(9) % 11) # 10 1
print((2 * math.factorial(26)) % 29) # 28A variant: Wilson's theorem with a composite modulus
If the modulus is composite, Wilson's theorem does not apply directly. Split the modulus into prime factors and use the Chinese Remainder Theorem, or note that the factorial already contains the modulus's factors.
Example 7. Find the remainder when 20! is divided by 21.
21 = 3 × 7. Since 20! contains both 3 and 7, it is divisible by 21. The remainder is 0.
Be careful when the modulus is a prime square such as 9 or 25. For n > 4 composite, (n − 1)! ≡ 0 (mod n) holds, but this is a fact about divisibility, not about Wilson's theorem.
Common errors
- Applying it to a composite modulus. Check that the divisor is prime before writing −1.
- Using it for n! rather than (p − 1)!. If n ≥ p, then n! is divisible by p and the remainder is 0. If n = p − 1, use Wilson. If n < p − 1, use the peeling method.
- Losing the sign. When you divide a congruence by a negative number, you need its inverse. Instead of dividing, test small values of x until the congruence holds.
- Reporting −1. A remainder must satisfy 0 ≤ r < p, so report p − 1.
Decision criteria
| Situation | Method |
|---|---|
| (p − 1)! mod prime p | Wilson: answer p − 1 |
| (p − 2)! mod prime p | Answer 1 |
| (p − 3)! or (p − 4)! mod p | Peel factors, use negative remainders, solve for the unknown |
| n! mod p with n ≥ p | Answer 0 |
| (n − 1)! mod composite n (n > 4) | Answer 0 |
| Expression mixing factorials | Reduce each term separately, then add |
Exercises
Exercise
What is the remainder when 18! is divided by 19?
Show answer
19 is prime, so 18! ≡ −1 (mod 19). The remainder is 18.
Exercise
What is the remainder when 22! is divided by 23, and what is the remainder when 21! is divided by 23?
Show answer
23 is prime. 22! ≡ −1, so the remainder is 22. Since 22! = 22 × 21! and 22 ≡ −1, 21! ≡ 1 (mod 23). The remainder is 1.
Exercise
Find the remainder when 11! is divided by 13.
Show answer
12! ≡ −1 (mod 13) and 12! = 12 × 11! ≡ −11!. So 11! ≡ 1... check: −11! ≡ −1 gives 11! ≡ 1. The remainder is 1.
Exercise
Find the remainder when 15! is divided by 19.
Show answer
18! ≡ −1 (mod 19). 18! = 18 × 17 × 16 × 15! ≡ (−1)(−2)(−3) × 15! = −6 × 15!. So −6 × 15! ≡ −1, which gives 6 × 15! ≡ 1 (mod 19). Since 6 × 16 = 96 = 5 × 19 + 1, the inverse of 6 is 16, so 15! ≡ 16. The remainder is 16.
Exercise
Find the remainder when 5! is divided by 6, and explain why Wilson's theorem does not give 5.
Show answer
5! = 120 = 6 × 20, so the remainder is 0. Wilson's theorem requires a prime modulus, and 6 is composite, so it does not apply.