Sub topic 11
Euler's theorem for composite moduli
This chapter assumes you can compute a remainder with congruence notation, reduce large powers by finding a cycle, and use Fermat's little theorem for a prime modulus. It also assumes you can compute Euler's totient function φ(n). Fermat's theorem only covers prime moduli, but CAT often asks for remainders on division by 15, 18, 36 or 100. Euler's theorem handles those composite moduli.
The statement
If gcd(a, n) = 1, then
a^φ(n) ≡ 1 (mod n)
Here φ(n) counts the integers from 1 to n that share no factor with n. For a prime p, φ(p) = p − 1, so Fermat's little theorem is the special case n = p.
To compute φ(n), factorise n into primes and apply
φ(n) = n × (1 − 1/p₁) × (1 − 1/p₂) × …
where p₁, p₂, … are the distinct primes dividing n.
| n | Prime factorisation | φ(n) |
|---|---|---|
| 12 | 2² × 3 | 12 × 1/2 × 2/3 = 4 |
| 15 | 3 × 5 | 15 × 2/3 × 4/5 = 8 |
| 18 | 2 × 3² | 18 × 1/2 × 2/3 = 6 |
| 36 | 2² × 3² | 36 × 1/2 × 2/3 = 12 |
| 100 | 2² × 5² | 100 × 1/2 × 4/5 = 40 |
| 120 | 2³ × 3 × 5 | 120 × 1/2 × 2/3 × 4/5 = 32 |
Only the distinct primes matter. The exponent on a prime changes n but not the list of fractions.
Why it works
Take every number from 1 to n that is coprime to n. There are φ(n) of them. Multiply each one by a, where a is also coprime to n. The products are again coprime to n, and they are all different mod n. So multiplying by a only shuffles the same set of remainders.
The product of the set therefore equals a^φ(n) times the product of the set (mod n). The product of the set is coprime to n, so you can cancel it. That leaves a^φ(n) ≡ 1.
Using it to reduce an exponent
Once a^φ(n) ≡ 1, any exponent can be replaced by its remainder on division by φ(n).
- Check gcd(a, n) = 1.
- Compute φ(n).
- Write the exponent as φ(n) × q + r.
- Then a^exponent ≡ a^r (mod n), because (a^φ(n))^q ≡ 1.
Example 1. Find the remainder when 5^100 is divided by 18.
- gcd(5, 18) = 1, and φ(18) = 6.
- 100 = 6 × 16 + 4, so 5^100 ≡ 5^4.
- 5² = 25 ≡ 7 (mod 18), and 7² = 49 ≡ 13 (mod 18).
The remainder is 13.
Example 2. Find the last two digits of 3^203.
The last two digits are the remainder mod 100.
- gcd(3, 100) = 1, and φ(100) = 40.
- 203 = 40 × 5 + 3, so 3^203 ≡ 3³ = 27 (mod 100).
The last two digits are 27.
Checking the theorem in code
The function below computes φ(n) with the product formula. A second function finds the true cycle length, and the loop compares it with φ(n).
def phi(n):
result, m, p = n, n, 2
while p * p <= m:
if m % p == 0:
while m % p == 0:
m //= p
result -= result // p
p += 1
if m > 1:
result -= result // m
return result
def order(a, n):
k = 1
while pow(a, k, n) != 1:
k += 1
return k
for a, n in [(7, 15), (5, 18), (3, 100)]:
print(a, n, phi(n), order(a, n), pow(a, phi(n), n))
print(pow(5, 100, 18)) # 13
print(pow(3, 203, 100)) # 27
print(pow(6, 50, 21)) # 15The loop prints these four values for each pair: a, n, φ(n), the true cycle length, and a^φ(n) mod n.
7 15 8 4 1
5 18 6 6 1
3 100 40 20 1The last column is always 1, as the theorem says. The cycle-length column is not always φ(n). It is 4 for 7 mod 15 and 20 for 3 mod 100.
φ(n) is a safe period, not always the shortest
The true cycle length always divides φ(n), but it can be smaller. Both of these statements are correct:
- 7^8 ≡ 1 (mod 15), by Euler.
- 7^4 ≡ 1 (mod 15), a shorter cycle.
Euler's theorem is the reliable tool when you do not want to search for a cycle. If a short cycle is easy to spot, such as 7⁴ = 2401 ≡ 1 (mod 100), use it. It gives a smaller exponent to work with.
When a and n share a factor
If gcd(a, n) > 1, Euler's theorem does not apply directly. Split n into coprime prime-power pieces, find the remainder on each piece, and recombine with the Chinese Remainder Theorem.
Example 3. Find the remainder when 6^50 is divided by 21.
Here gcd(6, 21) = 3, so Euler fails. Since 21 = 3 × 7, find the remainders mod 3 and mod 7 separately.
- Mod 3: 6 ≡ 0, so 6^50 ≡ 0.
- Mod 7: 6 ≡ −1, so 6^50 ≡ (−1)^50 = 1.
Now find x with x ≡ 0 (mod 3) and x ≡ 1 (mod 7). The numbers that are 1 mod 7 are 1, 8, 15, …, and 15 is the first that is divisible by 3. So the remainder is 15.
The decision flow is shown below.
Common errors
- Skipping the gcd check. Computing 6^50 mod 21 with φ(21) = 12 gives a wrong answer, because 6 and 21 are not coprime.
- Using n − 1 instead of φ(n). The formula φ(n) = n − 1 holds only for primes. For 15 the exponent period is 8, not 14.
- Counting prime powers wrongly. For n = 36, the distinct primes are 2 and 3, so φ(36) = 36 × 1/2 × 2/3 = 12. A common slip is to use 4 and 9 as if they were separate primes.
- Reducing the base's exponent incorrectly in towers. For 7^(9^9) mod 15, the exponent 9^9 is reduced mod φ(15) = 8, not mod 15. The modulus for the exponent is φ(n), not n.
- Treating the result as the shortest cycle. The exponent reduction is always valid. It just may not be the smallest.
Worked CAT-style problem
Example 4. Find the remainder when 11^200 is divided by 36.
- gcd(11, 36) = 1, so Euler applies, and φ(36) = 12.
- 200 = 12 × 16 + 8, so 11^200 ≡ 11^8 (mod 36).
- 11² = 121 ≡ 13 (mod 36), 11⁴ ≡ 13² = 169 ≡ 25, and 11⁸ ≡ 25² = 625 ≡ 13.
The remainder is 13.
Exercise
Find the remainder when 11^200 is divided by 36 using a different route than Example 4: split 36 into 4 and 9, find each remainder, and combine them. What do you get?
Show answer
Mod 4: 11 ≡ 3 ≡ −1, so 11^200 ≡ (−1)^200 = 1. Mod 9: φ(9) = 6 and 200 = 6 × 33 + 2, so 11^200 ≡ 11² = 121 ≡ 4 (mod 9). Now find x ≡ 1 (mod 4) and x ≡ 4 (mod 9). The numbers 4, 13, 22, 31 are 4 mod 9, and 13 is 1 mod 4. The remainder is 13, which matches Example 4.
Exercise
Why can you not use Euler's theorem with φ(21) = 12 to find 6^50 mod 21, and what is the remainder?
Show answer
gcd(6, 21) = 3, so 6 and 21 are not coprime, and the theorem does not apply. Split 21 into 3 and 7. Mod 3, 6^50 ≡ 0. Mod 7, 6 ≡ −1, so 6^50 ≡ 1. The number that is 0 mod 3 and 1 mod 7 below 21 is 15, so the remainder is 15.