Euler's totient function
Euler's totient function, also known as phi-function , counts the number of integers between 1 and inclusive, which are coprime to . Two numbers are coprime if their greatest common divisor equals ( is considered to be coprime to any number).
Here are values of for the first few positive integers:
Properties
The following properties of Euler totient function are sufficient to calculate it for any number:
- If is a prime number, then for all . Therefore we have:
- If is a prime number and , then there are exactly numbers between and that are divisible by . Which gives us:
If and are relatively prime, then:
This relation is not trivial to see. It follows from the Chinese remainder theorem. The Chinese remainder theorem guarantees, that for each and each , there exists a unique with and . It's not hard to show that is coprime to if and only if is coprime to and is coprime to . Therefore the amount of integers coprime to is equal to product of the amounts of and .
In general, for not coprime and , the equation
with holds.
Thus, using the first three properties, we can compute through the factorization of (decomposition of into a product of its prime factors). If , where are prime factors of ,
Implementation
Here is an implementation using factorization in :
int phi(int n) {
int result = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0)
n /= i;
result -= result / i;
}
}
if (n > 1)
result -= result / n;
return result;
}Euler totient function from to in
If we need the totient of all numbers between and , then factorizing all numbers is not efficient. We can use the same idea as the Sieve of Eratosthenes. It is still based on the property shown above, but instead of updating the temporary result for each prime factor for each number, we find all prime numbers and for each one update the temporary results of all numbers that are divisible by that prime number.
Since this approach is basically identical to the Sieve of Eratosthenes, the complexity will also be the same:
void phi_1_to_n(int n) {
vector<int> phi(n + 1);
for (int i = 0; i <= n; i++)
phi[i] = i;
for (int i = 2; i <= n; i++) {
if (phi[i] == i) {
for (int j = i; j <= n; j += i)
phi[j] -= phi[j] / i;
}
}
}Finding the totient from to using the segmented sieve
If we need the totient of all numbers between and , we can use the segmented sieve approach.
The algorithm first precomputes all primes up to using a linear sieve in time and space. For each number in the range , it then applies the factorization-based formula by iterating over these primes. We maintain a remainder array to track the unfactored part of each number. If a remainder is still greater than 1 after processing all small primes, it indicates a large prime factor greater than , which is handled in a final pass. The overall complexity for the range computation is .
const long long MAX_RANGE = 1e6 + 6;
vector<long long> primes;
long long phi[MAX_RANGE], rem[MAX_RANGE];
vector<int> linear_sieve(int n) {
vector<bool> composite(n + 1, 0);
vector<int> prime;
// 0 and 1 are not composite (nor prime)
composite[0] = composite[1] = 1;
for(int i = 2; i <= n; i++) {
if(!composite[i]) prime.push_back(i);
for(int j = 0; j < prime.size() && i * prime[j] <= n; j++) {
composite[i * prime[j]] = true;
if(i % prime[j] == 0) break;
}
}
return prime;
}
// To get the value of phi(x) for L <= x <= R, use phi[x - L].
void segmented_phi(long long L, long long R) {
for(long long i = L; i <= R; i++) {
rem[i - L] = i;
phi[i - L] = i;
}
for(long long i : primes) {
for(long long j = max(i * i, (L + i - 1) / i * i); j <= R; j += i) {
phi[j - L] -= phi[j - L] / i;
while(rem[j - L] % i == 0) rem[j - L] /= i;
}
}
for(long long i = 0; i < R - L + 1; i++) {
if(rem[i] > 1) phi[i] -= phi[i] / rem[i];
}
}Divisor sum property
This interesting property was established by Gauss:
Here the sum is over all positive divisors of .
For instance the divisors of 10 are 1, 2, 5 and 10. Hence .
Finding the totient from 1 to using the divisor sum property
The divisor sum property also allows us to compute the totient of all numbers between 1 and . This implementation is a little simpler than the previous implementation based on the Sieve of Eratosthenes, however also has a slightly worse complexity:
void phi_1_to_n(int n) {
vector<int> phi(n + 1);
phi[0] = 0;
phi[1] = 1;
for (int i = 2; i <= n; i++)
phi[i] = i - 1;
for (int i = 2; i <= n; i++)
for (int j = 2 * i; j <= n; j += i)
phi[j] -= phi[i];
}Application in Euler's theorem
The most famous and important property of Euler's totient function is expressed in Euler's theorem:
In the particular case when is prime, Euler's theorem turns into Fermat's little theorem:
Euler's theorem and Euler's totient function occur quite often in practical applications, for example both are used to compute the modular multiplicative inverse.
As immediate consequence we also get the equivalence:
This allows computing for very big , especially if is the result of another computation, as it allows to compute under a modulo.
Group Theory
is the order of the multiplicative group mod n , that is the group of units (elements with multiplicative inverses). The elements with multiplicative inverses are precisely those coprime to .
The multiplicative order of an element mod , denoted , is the smallest such that . is the size of the subgroup generated by , so by Lagrange's Theorem, the multiplicative order of any must divide . If the multiplicative order of is , the largest possible, then is a primitive root and the group is cyclic by definition.
Generalization
There is a less known version of the last equivalence, that allows computing efficiently for not coprime and . For arbitrary and :
Proof:
Let be common prime divisors of and , and their exponents in . With those we define , which makes coprime to . And let be the smallest number such that divides . Assuming , we can write:
The equivalence between the third and forth line follows from the fact that . Indeed if with , then with .
Since and are coprime, we can apply Euler's theorem and get the efficient (since is very small; in fact ) formula:
This formula is difficult to apply, but we can use it to analyze the behavior of . We can see that the sequence of powers enters a cycle of length after the first (or less) elements. divides (because and are coprime we have ), therefore we can also say that the period has length . And since , we can conclude the desired, much simpler, formula:
Practice Problems
- SPOJ #4141 "Euler Totient Function" [Difficulty: CakeWalk]
- UVA #10179 "Irreducible Basic Fractions" [Difficulty: Easy]
- UVA #10299 "Relatives" [Difficulty: Easy]
- UVA #11327 "Enumerating Rational Numbers" [Difficulty: Medium]
- TIMUS #1673 "Admission to Exam" [Difficulty: High]
- UVA 10990 - Another New Function
- Codechef - Golu and Sweetness
- SPOJ - LCM Sum
- GYM - Simple Calculations (F)
- UVA 13132 - Laser Mirrors
- SPOJ - GCDEX
- UVA 12995 - Farey Sequence
- SPOJ - Totient in Permutation (easy)
- LOJ - Mathematically Hard
- SPOJ - Totient Extreme
- SPOJ - Playing with GCD
- SPOJ - G Force
- SPOJ - Smallest Inverse Euler Totient Function
- Codeforces - Power Tower
- Kattis - Exponial
- LeetCode - 372. Super Pow
- Codeforces - The Holmes Children
- Codeforces - Small GCD