- Published on
Euler's Totient Function
- Authors

- Name
- Muhammad Hasan
- @muhammadhasan01
I am long fascinated by Euler's Totient Function, and so I wanted to write it here and share about it. Let's discuss the definition, use cases, and some proofs.
Definition
Euler's totient function (also called the Phi function denoted with the symbol or symbol) for a positive integer , counts the number of positive integers less than or equal that are coprime to .1
That is, for a positive integer , is the number of positive integer such that and .
For example, we would have since there are numbers less than or equal to that is coprime with which are .
Note: we would have since the only positive integer less than or equal to is and .
Use Cases
Euler Theorem
Euler's Theorem states that:
Let and be a positive integers such that they are relatively prime, i.e. , then:
For example, let's try computing .
Since and are relatively prime, we will have:
Therefore we will have:
So .
Divisor Sum
The property established by Gauss,2 is that:
where the sum is over all positive divisors of .
For example: when , then we will have , which is true.
The formula can also be derived from elementary arithmetic.3
Let's continue with the previous example, let and consider the positive fractions up to with denominator :
Let's put them in their lowest term:
Notice that for the denominator there are exactly fractions, e.g.:
More generally, for each denominator we will have there are such fractions in the list above.
Thus the set of fractions is split into subsets of size for each dividing .
A similar argument applies for any .
Formula
For a positive integer let be the distinct primes that divides , then we will have:
For example, we have for the primes that divides are and hence:
For the proof of this formula, we need to know some important facts
Phi is a multiplicative function
Let and be a coprime positive integer then we will have:
Proof for this fact is: Let be the set of integers which are coprime to , , and respectively.
That is , , and .
Then we need to proof that there is a bijection between the set and the set .
In simpler terms, we need to proof that each element in corresponds uniquely to an element in set , and vice versa.
This is essentially CRT i.e. Chinese Remainder Theorem which states that if you have a system of modular congruences with relatively prime modulo (in this case, and ), then there exists a unique solution modulo the product of the modulo ().
If is coprime to and is coprime to this bijection sends to with coprime to (Because has no divisor in common with or ).
Therefore there is a bijection between with
Thus having:
Computing Phi with a prime power argument
Let be a prime number and , then we will have:
The proof for this is quite simple, the only way a positive integer having is that does not divides .
Therefore would not be in the set which have numbers.
So .
Proof for Euler Totient's Function Formula
Now that we now some important facts, we can prove the Euler Totient's Function Formula.
Let be a positive integer, then we can represent using prime factorization as follows:
Where is prime and is a positive integer for .
We can rewrite it as:
Using the facts of multiplicative function and phi with a prime power argument, we get:
Bonus (Code)
Here are C++ implementations for computing Euler's Totient Function:
int phi(int n) {
int result = n;
for (int p = 2; p * p <= n; p++) {
if (n % p == 0) {
while (n % p == 0) {
n /= p;
}
result -= result / p;
}
}
if (n > 1) {
result -= result / n;
}
return result;
}
Or we can also precompute using similar to Sieve Eratosthenes Trick:
const int N = 100000;
vector<int> phi(N + 1);
for (int i = 1; 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;
}
}
}