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 φ)

φ(n) = how many integers from 1 to n are coprime to n

Let's try another!

Example: 12

Start with the integers 1 to 12:

1 2 3 4 5 6 7 8 9 10 11 12

12 has the prime factors of 2 and 3, so we can cross off any multiple of 2 or 3:

1 2 3 4 5 6 7 8 9 10 11 12

We are left with 4 integers that are coprime to 12:

Answer: 4

Your turn!

Try 15:

Start with:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

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)

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:

1 2 3 4 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:

12345
678910
1112131415
1617181920
2122232425

We get the same thing 5 times.

φ(25) = 5 × φ(5)
= 5 × 4
= 20

And we get:

just one prime factor:(p − 1)
two of the same prime factorp(p − 1)
three of the same prime factorp2(p − 1)
"a" of the same prime factorpa-1(p − 1)

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14

We can also eliminate all multiples of 7, there are 14/7 = 2 of those:

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14

φ(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:

So we had this: n − ( np + nq − 1)
Let's remove brackets: n − npnq + 1
Multiply by pqn (=1): pq − q − p + 1
Which simplifies to: (p − 1)(q − 1)

The result is remarkably simple:

φ(pq) = (p − 1)(q − 1)
(where p and q are distinct primes)

So far we have discovered:

In fact it works generally:

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.

φ(30) = (p − 1)(q − 1)(r − 1)
= (2 − 1)(3 − 1)(5 − 1)
= 1 × 2 × 4
= 8

Lastly we multiply by the 2 we set aside at the start:

φ(60) = 16

So the algorithm is:

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.

φ(60) = 60 × ( 2−12 )( 3−13 )( 5−15 )
φ(60) = 60 × ( 12 )( 23 )( 45 )
= 16

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)

φ(60) = 2×2×3×5 × (12)(23)(45)
= 2×2×3×5 × (12)(23)(45)
= 16

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:

φ(60) = 22−1(2 − 1)31−1(3 − 1)51−1(5 − 1)
= 21(1)30(2)50(4)
= 2 × 2 × 4
= 16

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:

123456
789101112
131415161718
192021222324
252627282930
313233343536
373839404142

Eliminate all entries that are not coprime to 6, in other words any with factors of 2 or 3:

123456
789101112
131415161718
192021222324
252627282930
313233343536
373839404142

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:

123456
789101112
131415161718
192021222324
252627282930
313233343536
373839404142

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).

dog carries message

Uses

Euler's Totient Function is very useful in Number Theory and plays a central role in RSA Cryptography

Summary

φ(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)

φ(pq) = φ(p)φ(q)

 

25373, 25374, 25375, 25376, 25377, 25378, 25379, 25380, 25381, 25382