Sub topic 9
Fermat's Little Theorem
This chapter assumes you can already use congruence notation, reduce products and sums of remainders, and find the cycle of a power's remainders. It also assumes you can recognise a prime number. Fermat's little theorem turns the cycle idea into a guaranteed rule for prime divisors. It also gives you a fast way to cut a huge exponent down to a small one.
The statement
Let p be a prime and a any integer. Then
a^p ≡ a (mod p).
If p does not divide a, you can divide both sides by a and get the form you will use most often:
a^(p−1) ≡ 1 (mod p).
In words: raise a number that is not a multiple of a prime p to the power p − 1, and the remainder on division by p is always 1.
A quick check with p = 7, where p − 1 = 6:
| a | a^6 | a^6 mod 7 |
|---|---|---|
| 2 | 64 | 1 (64 = 63 + 1) |
| 3 | 729 | 1 (729 = 728 + 1) |
| 5 | 15625 | 1 (15625 = 15624 + 1) |
The same check in Python, for several primes at once:
for p in [5, 7, 11, 13]:
for a in range(1, p):
assert pow(a, p - 1, p) == 1
print("a^(p-1) = 1 (mod p) holds for every a not divisible by p")Why it works
You do not need the proof for CAT, but the idea explains the conditions. Take the numbers 1, 2, ..., p − 1 and multiply each by a, where p does not divide a. The products a, 2a, ..., (p−1)a all leave different non-zero remainders mod p. They are a rearrangement of 1, 2, ..., p − 1. Multiplying everything together gives
a^(p−1) · (p−1)! ≡ (p−1)! (mod p).
Since (p−1)! has no factor p, you can cancel it. That leaves a^(p−1) ≡ 1.
Two conditions come out of this argument. The modulus must be prime, so that no product becomes 0 or repeats a remainder. The base must not be a multiple of p, so that you may cancel.
Reducing a large exponent
The main use is this. Because a^(p−1) ≡ 1, every block of p − 1 factors of a can be replaced by 1. Only the leftover exponent matters.
- Check that the divisor p is prime.
- Check that p does not divide the base a. If it does, the remainder is 0.
- Replace the exponent n by n mod (p − 1).
- Compute the small power.
Example 1. Find the remainder when 2^100 is divided by 13.
Here p = 13, so p − 1 = 12. Since 100 = 12 × 8 + 4, we have 2^100 ≡ 2^4 = 16 ≡ 3 (mod 13).
print(pow(2, 100, 13)) # 3
print(pow(2, 100 % 12, 13)) # 3Example 2. Find the remainder when 3^50 is divided by 7.
Since p − 1 = 6 and 50 = 6 × 8 + 2, we get 3^50 ≡ 3^2 = 9 ≡ 2 (mod 7).
Example 3. Find the remainder when 7^100 is divided by 5.
First reduce the base: 7 ≡ 2 (mod 5). Here p − 1 = 4 and 100 is a multiple of 4. So 2^100 ≡ (2^4)^25 ≡ 1^25 = 1. This matches the cycle method from the earlier chapter, where the remainders of powers of 2 repeat as 2, 4, 3, 1.
print(pow(7, 100, 5)) # 1Example 4. The exponent can itself be a power. Find the remainder when 5^(3^4) is divided by 7.
The exponent is 3^4 = 81. Since 81 = 6 × 13 + 3, we get 5^81 ≡ 5^3 = 125 ≡ −1 ≡ 6 (mod 7). Here the negative remainder shortcut finishes the job, because 125 = 126 − 1 and 126 is a multiple of 7.
print(pow(5, 3**4, 7)) # 6When the base is a multiple of p
If p divides a, the theorem in the form a^(p−1) ≡ 1 does not apply, and every positive power of a leaves remainder 0. For example, 14^100 mod 7 = 0. The weaker form a^p ≡ a (mod p) still holds, because both sides are 0.
When the exponent is a multiple of p − 1
If n mod (p − 1) = 0, the reduced exponent is 0, and the remainder is 1. It is not 0. In Example 3 the answer was 1, not 0, for this reason.
The form a^p ≡ a
The form a^p ≡ a (mod p) works when the exponent is exactly p, with no condition on a. It proves that n^p − n is divisible by p for every integer n.
- 5^13 leaves remainder 5 on division by 13.
- n^7 − n is divisible by 7 for every integer n. For n = 2: 128 − 2 = 126 = 7 × 18.
Fermat's theorem and cycles
The theorem says the cycle of remainders for a^k mod p returns to 1 no later than k = p − 1. The actual cycle can be shorter, but its length always divides p − 1.
| Powers of | mod | Cycle length | Divides p − 1? |
|---|---|---|---|
| 2 | 7 | 3 (2, 4, 1) | yes, 6 = 3 × 2 |
| 3 | 7 | 6 (3, 2, 6, 4, 5, 1) | yes |
| 2 | 13 | 12 | yes |
| 7 | 5 | 4 (2, 4, 3, 1) | yes |
So Fermat's theorem is a safe upper bound. If you spot a shorter cycle quickly, use it. If you do not, p − 1 always works.
Using the theorem to find an inverse
Multiplying a^(p−2) by a gives a^(p−1) ≡ 1. So a^(p−2) is the modular inverse of a: the number that multiplies with a to give remainder 1.
For p = 7 and a = 3: 3^5 = 243 ≡ 5 (mod 7), and 3 × 5 = 15 ≡ 1. This lets you "divide" in a remainder problem. For example, to solve 3x ≡ 4 (mod 7), multiply both sides by 5 to get x ≡ 20 ≡ 6.
p, a = 7, 3
inv = pow(a, p - 2, p)
print(inv, (a * inv) % p) # 5 1
print((inv * 4) % p) # 6Choosing between methods
Common errors
- Using it with a composite divisor. The statement a^(m−1) ≡ 1 is false in general when m is not prime. For m = 9: 2^8 = 256 = 252 + 4, so the remainder is 4, not 1. For composite moduli you need Euler's theorem, covered in a later chapter.
- Reducing the exponent mod p instead of mod p − 1. For 2^100 mod 13, the correct reduced exponent is 100 mod 12 = 4. Using 100 mod 13 = 9 gives 2^9 = 512 ≡ 5, which is wrong.
- Forgetting to reduce the base first. For 100^50 mod 7, first write 100 ≡ 2, then reduce the exponent.
- Missing the multiple-of-p case. If the base is divisible by p, the answer is 0, not 1.
- Answering 0 when the reduced exponent is 0. A zero exponent means the answer is 1 (for a not divisible by p).
- Treating a large divisor as prime without checking. Test it by trial division by primes up to its square root. For example, 91 = 7 × 13 is not prime.
# Composite modulus: the Fermat rule fails
print(pow(2, 8, 9)) # 4, not 1
# Wrong reduction (mod p) versus right reduction (mod p - 1)
print(pow(2, 100, 13)) # 3
print(pow(2, 9, 13)) # 5 (wrong exponent)
print(pow(2, 4, 13)) # 3 (right exponent)Practice
Exercise
What is the remainder when 3^200 is divided by 11?
Show answer
The divisor 11 is prime and does not divide 3, so 3^10 ≡ 1 (mod 11). Since 200 is a multiple of 10, 3^200 = (3^10)^20 ≡ 1. The remainder is 1.
Exercise
What is the remainder when 2^50 + 3^50 is divided by 7?
Show answer
Here p − 1 = 6 and 50 = 6 × 8 + 2. So 2^50 ≡ 2^2 = 4 and 3^50 ≡ 3^2 = 9 ≡ 2 (mod 7). The sum of remainders is 4 + 2 = 6, so the remainder is 6.
Exercise
Find the remainder when 6^100 is divided by 7, and the remainder when 21^100 is divided by 7.
Show answer
For 6^100, note 6 ≡ −1 (mod 7), so 6^100 ≡ (−1)^100 = 1. Fermat agrees: 100 = 6 × 16 + 4, and 6^4 = 1296 = 7 × 185 + 1, so the remainder is 1. For 21^100, the base 21 is a multiple of 7, so the remainder is 0.