Euler's Totient Function
Also called the Phi function after the Greek letter φ
How many integers from 1 to n don't share any prime factors with n?
Example: 6
6 has the prime factors of 2 and 3
Let's check each integer 1 to n:
- 1 does not have factors 2 or 3
- 2 has the factor 2
- 3 has the factor 3
- 4 has the factor 2
- 5 does not have factors 2 or 3
- 6 has the factors 2 and 3
So there are only 2 integers without the factors 2 or 3
Answer: 2
The idea "don't share any prime factors" is often shortened to "coprime" or "relatively prime".
And so we get Euler's Totient Function (symbol φ)
Let's try another!
Example: 12
Start with the integers 1 to 12:
12 has the prime factors of 2 and 3, so we can cross off any multiple of 2 or 3:
We are left with 4 integers that are coprime to 12:
Answer: 4
Your turn!
Try 15:
Start with:
15 has the prime factors of 3 and 5, the rest is up to you.
(We got 8 integers coprime to 15.)
These are all examples of Euler's Totient Function, which has the symbol φ (the Greek letter Phi)
- φ(6) = 2
- φ(12) = 4
- φ(15) = 8
It is that simple, just crossing numbers off a list.
But it can take a long time of course, so any timesavers would be handy. Let's discover some!
Prime Numbers
What happens with prime numbers?
Example: 5
5 is prime. It has only one prime factor: itself!
So all other numbers in the list are coprime to 5:
Answer:
φ(5) = 4
This is generally true of all prime numbers:
φ(p) = p − 1
(where p is a prime number)
We have our first timesaver.
Same Prime More Than Once
What if we multiply by the same prime again?
Example: 25
25 is 5 × 5:
We get the same thing 5 times.
φ(25) = 5 × φ(5)
= 5 × 4
= 20
And we get:
We have our next timesaver:
φ(pa) = pa-1(p − 1)
(where p is a prime number)
Example: 81
81 is 34
φ(pa) = pa-1(p − 1)
φ(34) = 34-1(3 − 1)
= 33(2)
= 27 × 2
= 54
Two Different Primes
What about two different primes multiplied together?
Example: 14
14 is 2 times 7
We can eliminate all multiples of 2, there are 14/2 = 7 of those:
We can also eliminate all multiples of 7, there are 14/7 = 2 of those:
So far we have eliminated 7+2 = 9, but oops, we counted 14 twice!
So in fact we only eliminated 8, leaving 14 − 8 = 6 that are coprime:
φ(14) = 6
This is generally true! We can work out how many of each prime factor we eliminated, adjust for over counting, and then work out how many are left.
And we can make a formula for it:
We started with n=pq, eliminated n/p numbers, then n/q numbers, and adjusted by 1:
The result is remarkably simple:
φ(pq) = (p − 1)(q − 1)
(where p and q are distinct primes)
So far we have discovered:
- φ(p) = (p − 1)
- φ(pq) = (p − 1)(q − 1)
In fact it works generally:
- φ(pqr) = (p − 1)(q − 1)(r − 1)
- etc...
But all primes must be distinct (different)!
What about a mix of primes, some different, some the same?
Example: 60
The prime factors of 60 are 2, 2, 3 and 5.
2 appears twice, so let's set the extra 2 aside for the moment.
Lastly we multiply by the 2 we set aside at the start:
φ(60) = 16
So the algorithm is:
- Break the number into its prime factors
- Set aside any duplicate factors
- Use φ(pqr...) = (p − 1)(q − 1)(r − 1)... on the remaining factors
- Finish up by multiplying by the factors that were set aside
A better version!
But there is a neat formula that does all that and makes good sense:
φ(n) = n (p1 − 1p1)(p2 − 1p2) ... (pz − 1pz)
It starts with n, then multiplies it in a special way to reduce it like we did with the eliminations.
Example: 60 (again)
The distinct prime factors of 60 are 2, 3 and 5.
What did we do? We started with 60, then:
- eliminated every factor of 2, which is the same as multiplying by 1/2
- eliminated every factor of 3, which is the same as multiplying by 2/3
- eliminated every factor of 5, which is the same as multiplying by 4/5
And because we apply these one after the other there is no risk of overcounting!
If you are doing the calculation manually, try replacing n with all its factors, it makes cancelling easier:
Example: 60 (again again)
Note the formula is often written in this (exactly equivalent) form:
φ(n) = n (1 − 1p1)(1 − 1p2) ... (1 − 1pz)
Any of those formulas let us find φ(n) for any integer above 1, because integers above 1 are either prime or the product of primes. See Fundamental Theorem of Arithmetic.
Another formula
There is another clever general formula:
φ(n) = p1k1−1(p1 − 1)p2k2−1(p2 − 1) ... pzkz−1(pz − 1)
Where p1, p2, etc are distinct (different) primes, and the exponents k1, k2, etc are their exponents.
Using distinct primes and their exponents neatly handles single or multiple primes. Let us do our previous example again to see:
Example: 60 (once more)
The prime factors of 60 are 2, 2, 3 and 5, and using exponents we get:
60 = 22 × 3 × 5
The formula for three distinct primes:
φ(n) = p1k1−1(p1 − 1)p2k2−1(p2 − 1)p3k3−1(p3 − 1)
Substituting p1=2, k1=2, p2=3, k2=1, p3=5, k3=1:
Do you see how the 2 had an exponent of 1 (21=2) so the extra 2 doubles the result, but the other primes had an exponent of 0 (30=1 and 50=1) and they get ignored.
Multiplicative Property
This is also true:
φ(pq) = φ(p)φ(q)
(where p and q are coprime, so don't share any factors)
To show why this works, let us see an example:
Example: 6×7 = 42
Laid out as a 6×7 table:
1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 |
31 | 32 | 33 | 34 | 35 | 36 |
37 | 38 | 39 | 40 | 41 | 42 |
Eliminate all entries that are not coprime to 6, in other words any with factors of 2 or 3:
1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 |
31 | 32 | 33 | 34 | 35 | 36 |
37 | 38 | 39 | 40 | 41 | 42 |
We are left with exactly φ(6) = 2 columns.
Next: eliminate all entries that are not coprime to 7, in other words any with a factor of 7:
1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 |
31 | 32 | 33 | 34 | 35 | 36 |
37 | 38 | 39 | 40 | 41 | 42 |
Each column ends up with exactly φ(7) = 6 entries.
So a 6×7 table ends up as φ(6)=2 by φ(7)=6
And our answer is that φ(6×7) = φ(6)×φ(7) = 2×6 = 12
Try this yourself with 3×5, or maybe 5×7, just make sure the two factors are coprime (no shared factors).
Summary
- Euler's Totient Function φ is how many integers from 1 to n are coprime to n
- Coprime means "do not share any factors"
- For a prime number: φ(p) = p − 1
- For powers of a prime number: φ(pa) = pa-1(p − 1)
- General formulas (pr are prime numbers):
φ(n) = n (p1 − 1p1)(p2 − 1p2) ... (pz − 1pz)
φ(n) = n (1 − 1p1)(1 − 1p2) ... (1 − 1pz)
φ(n) = p1k1−1(p1 − 1)p2k2−1(p2 − 1) ... pzkz−1(pz − 1)
- Multiplicative property (p and q are coprime):
φ(pq) = φ(p)φ(q)