Sub topic 10
Euler's Totient Function
Prerequisites and purpose
Assume you can divide integers, factor numbers into primes, and work with remainders. You should also know that \(a\) and \(n\) are coprime when their greatest common divisor is 1. For example, 8 and 15 are coprime because they share no factor greater than 1.
Euler's totient function counts how many positive integers up to \(n\) are coprime to \(n\). It is written as \(\varphi(n)\), pronounced “phi of \(n\).”
For example, the positive integers from 1 to 10 that are coprime to 10 are:
1, 3, 7, 9
Therefore, \(\varphi(10)=4\).
Computing \(\varphi(n)\)
If \(p\) is prime, every number from 1 to \(p-1\) is coprime to \(p\). Thus:
\[ \varphi(p)=p-1 \]
For a prime power \(p^k\), the numbers that are not coprime to \(p^k\) are exactly the multiples of \(p\). This gives:
\[ \varphi(p^k)=p^k-p^{k-1} =p^k\left(1-\frac{1}{p}\right) \]
For a general number, factor it into distinct primes:
\[ n=p1^{a1}p2^{a2}\cdots pr^{ar} \]
Then use:
\[ \varphi(n)=n\left(1-\frac{1}{p1}\right) \left(1-\frac{1}{p2}\right)\cdots \left(1-\frac{1}{p_r}\right) \]
Only distinct prime factors appear in the product. Their exponents do not create additional factors.
| \(n\) | Prime factorisation | Calculation | \(\varphi(n)\) |
|---|---|---|---|
| 12 | \(2^2\cdot3\) | \(12(1-\frac12)(1-\frac13)\) | 4 |
| 18 | \(2\cdot3^2\) | \(18(1-\frac12)(1-\frac13)\) | 6 |
| 25 | \(5^2\) | \(25(1-\frac15)\) | 20 |
from math import gcd
def phi_by_count(n):
return sum(gcd(k, n) == 1 for k in range(1, n + 1))
print(phi_by_count(12)) # 4
print(phi_by_count(18)) # 6Why phi matters for powers
Euler's theorem uses the totient function:
\[ a^{\varphi(n)}\equiv1\pmod n \]
This statement is valid when \(a\) and \(n\) are coprime. It lets you reduce a large exponent modulo \(\varphi(n)\).
For example, find \(3^{100}\pmod{10}\). Since \(\gcd(3,10)=1\), and \(\varphi(10)=4\):
\[ 3^4\equiv1\pmod{10} \]
Now reduce the exponent:
\[ 100\equiv0\pmod4 \]
Therefore:
\[ 3^{100}=(3^4)^{25}\equiv1^{25}\equiv1\pmod{10} \]
A common error is applying Euler's theorem when the base and modulus are not coprime. For instance, \(\gcd(6,10)=2\), so the theorem cannot directly reduce powers of 6 modulo 10.
Exercise
Find \(\varphi(36)\), then state whether Euler's theorem can be applied to \(5^{100}\pmod{36}\).
Show answer
\(36=2^2\cdot3^2\), so \(\varphi(36)=36(1-\frac12)(1-\frac13)=12\). Yes, because \(\gcd(5,36)=1\).