Sub topic 10
Euler's totient function
This chapter defines Euler's totient function, φ(n), and shows how to compute it quickly from a prime factorisation. It also covers the counting questions CAT sets on it. The next chapter uses φ(n) to state Euler's theorem. Here the goal is only to be fast and accurate with φ itself.
I assume you can factorise numbers into primes, compute a gcd, and use the remainder notation from earlier chapters.
What the function counts
Two integers are coprime if their greatest common divisor is 1. The totient φ(n) is the number of integers k with 1 ≤ k ≤ n that are coprime to n.
Take n = 12. Check each k from 1 to 12 against 12:
- 1, 5, 7 and 11 have gcd 1 with 12, so they count.
- 2, 3, 4, 6, 8, 9, 10 and 12 share a factor of 2 or 3 with 12, so they do not.
So φ(12) = 4.
Two small cases to remember:
- φ(1) = 1, because gcd(1, 1) = 1.
- For a prime p, every k from 1 to p − 1 is coprime to p, and p itself is not. So φ(p) = p − 1.
Prime powers
For a prime power p^k, the only integers up to p^k that share a factor with it are the multiples of p. There are p^(k−1) of them, so:
φ(p^k) = p^k − p^(k−1) = p^(k−1)(p − 1)
Examples:
- φ(2^10) = 2^10 − 2^9 = 512.
- φ(7^2) = 49 − 7 = 42.
- φ(3^4) = 81 − 27 = 54.
The general formula
φ is multiplicative. If gcd(m, n) = 1, then φ(mn) = φ(m)·φ(n). Splitting n into prime powers, which are pairwise coprime, gives the working formula. If n = p₁^a · p₂^b · p₃^c ⋯, then:
φ(n) = n · (1 − 1/p₁)(1 − 1/p₂)(1 − 1/p₃) ⋯
Only the distinct primes appear in the product. Their exponents do not.
Cancel as you go. Divide n by p first, then multiply by p − 1.
| n | Factorisation | Calculation | φ(n) |
|---|---|---|---|
| 12 | 2² · 3 | 12 · ½ · ⅔ | 4 |
| 36 | 2² · 3² | 36 · ½ · ⅔ | 12 |
| 100 | 2² · 5² | 100 · ½ · ⅘ | 40 |
| 360 | 2³ · 3² · 5 | 360 · ½ · ⅔ · ⅘ | 96 |
| 1001 | 7 · 11 · 13 | 6 · 10 · 12 | 720 |
For the last row, the three primes appear once each, so φ(n) is simply the product of (p − 1) over the primes.
Checking the formula in code
The brute-force function counts coprime integers directly. The fast function applies the formula by trial division. Running both on the same inputs confirms they agree.
from math import gcd
def phi_brute(n):
return sum(1 for k in range(1, n + 1) if gcd(k, n) == 1)
def phi(n):
result = n
m = n
p = 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
for n in [1, 12, 36, 97, 360, 1001]:
print(n, phi(n), phi_brute(n))The output is:
1 1 1
12 4 4
36 12 12
97 96 96
360 96 96
1001 720 720Each time a prime p divides n, the line result -= result // p multiplies the running value by (1 − 1/p) while staying in whole numbers. The leftover m > 1 is a single large prime factor that the loop did not divide out.
Counting questions CAT asks
Integers coprime to n
"How many positive integers up to 100 are coprime to 100?" This is φ(100) = 40.
If the range is a multiple of n, the pattern repeats in every block of n consecutive integers. The number of integers from 1 to 1000 that are divisible by neither 2 nor 5 is 1000 · ½ · ⅘ = 400. This is φ(10) = 4 per block of 10, times 100 blocks.
Integers not coprime to n
Subtract from the total. For n = 200 = 2³ · 5², φ(200) = 200 · ½ · ⅘ = 80. So 200 − 80 = 120 integers from 1 to 200 share a factor with 200.
Fractions in lowest terms
The fraction k/n is in lowest terms exactly when gcd(k, n) = 1. So the number of fractions k/36 with 1 ≤ k ≤ 36 that cannot be reduced is φ(36) = 12.
Sum of the coprime integers
For n > 2, the integers coprime to n pair up as k and n − k. Each pair sums to n, and there are φ(n)/2 pairs. So:
sum of integers below n that are coprime to n = n · φ(n) / 2
For n = 12 this gives 12 · 4 / 2 = 24, and indeed 1 + 5 + 7 + 11 = 24. For n = 100 it gives 100 · 40 / 2 = 2000.
Number of divisors with a given property
Another common question asks how many numbers up to n have gcd exactly 1 with n, or how many of the numbers 1 to n share a factor with it. Both reduce to φ(n) or n − φ(n). Read the wording carefully to see whether the range includes n itself. n is coprime to itself only when n = 1.
Common errors
- Using a prime's exponent in the product. For 360 = 2³ · 3² · 5, only 2, 3 and 5 appear in the product. Writing (1 − 1/2)³ is wrong.
- Using multiplicativity on non-coprime factors. φ(12) = 4, but φ(2) · φ(6) = 1 · 2 = 2. The factors 2 and 6 share a prime, so the rule does not apply. Split 12 as 4 · 3 instead: φ(4) · φ(3) = 2 · 2 = 4.
- Writing φ(p^k) = p^k − 1. The correct value is p^k − p^(k−1). The numbers to remove are the multiples of p, not every number below p^k.
- Forgetting that the formula needs prime factors. φ(n) = n · (1 − 1/a) for a composite a is wrong. Use primes only.
- Counting the wrong range. A question that asks for integers "less than n" excludes n. For n > 1, n is not coprime to itself, so the count is still φ(n). For n = 1 the two readings differ.
Why this matters for the next chapter
Euler's theorem says that for gcd(a, n) = 1, a^φ(n) leaves remainder 1 on division by n. To use it, you need φ(n) quickly. For example, to reduce 7^100 mod 12 you first compute φ(12) = 4. Practise the factorisation step until it takes seconds.
Exercise
Find φ(180), then find how many integers from 1 to 180 share a common factor greater than 1 with 180.
Show answer
180 = 2² · 3² · 5. φ(180) = 180 · ½ · ⅔ · ⅘ = 48. The number of integers from 1 to 180 that share a factor with 180 is 180 − 48 = 132.
Exercise
Find the sum of all positive integers less than 45 that are coprime to 45.
Show answer
45 = 3² · 5, so φ(45) = 45 · ⅔ · ⅘ = 24. The sum is 45 · 24 / 2 = 540.
Exercise
A student claims that φ(18) = φ(3) · φ(6). Compute both sides and explain the result.
Show answer
φ(18) = 18 · ½ · ⅔ = 6. φ(3) = 2 and φ(6) = 2, so the right side is 4. The claim fails because 3 and 6 are not coprime, so multiplicativity does not apply. A valid split is 18 = 2 · 9, giving φ(2) · φ(9) = 1 · 6 = 6.