Definition
Euler's totient function, denoted by , counts the integers less than or equal to that are coprime to .
For example, .
When is prime, clearly .
Properties
-
The Euler totient function is a multiplicative function.
That is, for any integers satisfying , written as , we have .
In particular, when is odd, .
See composition of residue systems for a proof.
-
.
???+ note "Proof" This follows from Möbius inversion and related facts.
It can also be seen as follows: if $\gcd(k, n) = d$, then $\gcd(\dfrac{k}{d},\dfrac{n}{d}) = 1, ( k < n )$. Let $f(x)$ denote the number of integers satisfying $\gcd(k, n) = x$. Then $n = \sum_{i = 1}^n{f(i)}$. From the argument above, we find that $f(x) = \varphi(\dfrac{n}{x})$, and hence $n = \sum_{d \mid n}\varphi(\dfrac{n}{d})$. Since the divisors $d$ and $\dfrac{n}{d}$ are symmetric, this becomes $n = \sum_{d \mid n}\varphi(d)$. -
If , where is prime, then . (This follows directly from the definition.)
-
By the fundamental theorem of arithmetic, let , where each is prime. Then .
???+ note "Proof" - Lemma: for any prime , .
Proof: among the integers from 1 to $p^k$, exactly $p^{k-1}$ are multiples of $p$; all the others are coprime to $p^k$. Thus $\varphi(p^k)=p^k-p^{k-1}=p^{k-1}\times(p-1)$. We next prove $\varphi(n) = n \times \prod_{i = 1}^s{\dfrac{p_i - 1}{p_i}}$. By the fundamental theorem of arithmetic and the multiplicativity of $\varphi(x)$, $$ \begin{aligned} \varphi(n) &= \prod_{i=1}^{s} \varphi(p_i^{k_i}) \\ &= \prod_{i=1}^{s} (p_i-1)\times {p_i}^{k_i-1}\\ &=\prod_{i=1}^{s} {p_i}^{k_i} \times(1 - \frac{1}{p_i})\\ &=n~ \prod_{i=1}^{s} (1- \frac{1}{p_i}) &\square \end{aligned} $$ -
For any integers that are not both , written as , .
This follows by direct calculation from the preceding identity.
Implementation
If only the totient of one number is needed, factor the number according to the definition while computing the answer. This process can be optimized with the Pollard Rho algorithm.
Reference implementation
C++
#include <cmath>
int euler_phi(int n) {
int ans = n;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) {
ans = ans / i * (i - 1);
while (n % i == 0) n /= i;
}
if (n > 1) ans = ans / n * (n - 1);
return ans;
}
Python
import math
def euler_phi(n):
ans = n
for i in range(2, math.isqrt(n) + 1):
if n % i == 0:
ans = ans // i * (i - 1)
while n % i == 0:
n = n // i
if n > 1:
ans = ans // n * (n - 1)
return ans
For the totients of multiple numbers, use the linear sieve introduced later.
For details, see sieving for the Euler totient function.
Applications
The Euler totient function is often used to simplify sums of greatest common divisors. Some Chinese articles call this Euler inversion1.
In the conclusion
Substituting into it gives
where is an Iverson bracket. Summing the identity yields
The key observation is : between and , the number of integers divisible by , indexed by , is .
Using this identity, we can enumerate the divisors and sum over them. For multiple queries, precompute prefix sums of the Euler totient function and use number-theoretic block decomposition.
GCD SUM
Given , find
Idea
Following the derivation above, we obtain
We need to compute the Euler totient function from through ; a linear sieve does this in and gives the answer.
Euler's Theorem
A theorem closely related to the Euler totient function is Euler's theorem. It states:
If , then .
Extended Euler Theorem
The extended Euler theorem handles the general case of and .
For the proof and exercises, see Euler's theorem.
Exercises
- SPOJ ETF. Euler Totient Function
- UVa 10179. Irreducible Basic Fractions
- UVa 10299. Relatives
- UVa 11327. Enumerating Rational Numbers
- TIMUS 1673. Admission to Exam
- Luogu P1390 Sum of Divisors
- Luogu P2155 [SDOI2008] The Salad Princess's Confusion
- Luogu P2568 GCD
References and Notes
Footnotes
-
This terminology does not appear in academic journals or foreign forums; use it with caution. ↩