logo
Published on

Euler's Totient Function

Authors

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 ϕ\phi symbol or φ\varphi symbol) for a positive integer nn, counts the number of positive integers less than or equal nn that are coprime to nn.1

That is, for a positive integer nn, ϕ(n)\phi(n) is the number of positive integer xx such that 1xn1 \leq x \leq n and gcd(n,x)=1\mathrm{gcd}(n, x) = 1.

For example, we would have ϕ(10)=4\phi(10) = 4 since there are 44 numbers less than or equal to 1010 that is coprime with 1010 which are (1,3,7,9)(1, 3, 7, 9).

Note: we would have ϕ(1)=1\phi(1) = 1 since the only positive integer less than or equal to 11 is 11 and gcd(1,1)=1\mathrm{gcd}(1, 1) = 1.

Use Cases

Euler Theorem

Euler's Theorem states that:

Let nn and aa be a positive integers such that they are relatively prime, i.e. gcd(a,n)=1\mathrm{gcd}(a, n) = 1, then:

aϕ(n)1 (mod n)a^{\phi(n)} \equiv 1 \ (\text{mod } n)

For example, let's try computing 20232020 mod 102023^{2020} \ \text{mod } 10.

Since 20232023 and 1010 are relatively prime, we will have:

2023ϕ(10)1 (mod 10)    202341 (mod 10)2023^{\phi(10)} \equiv 1 \ (\text{mod } 10) \implies 2023^{4} \equiv 1 \ (\text{mod } 10)

Therefore we will have:

(20234)5051505 (mod 10)    202320201 (mod 10)\left(2023^{4}\right)^{505} \equiv 1^{505} \ (\text{mod } 10) \implies 2023^{2020} \equiv 1 \ (\text{mod } 10)

So 20232020 mod 10=12023^{2020} \ \text{mod } 10 = 1.

Divisor Sum

The property established by Gauss,2 is that:

dnϕ(d)=n\displaystyle \sum_{d | n}\phi(d) = n

where the sum is over all positive divisors dd of nn.

For example: when n=10n = 10, then we will have ϕ(1)+ϕ(2)+ϕ(5)+ϕ(10)=10\phi(1) + \phi(2) + \phi(5) + \phi(10) = 10, which is true.

The formula can also be derived from elementary arithmetic.3

Let's continue with the previous example, let n=10n=10 and consider the positive fractions up to 11 with denominator 1010:

110,210,310,410,510,610,710,810,910,1010\displaystyle \frac{1}{10}, \frac{2}{10}, \frac{3}{10}, \frac{4}{10}, \frac{5}{10}, \frac{6}{10}, \frac{7}{10}, \frac{8}{10}, \frac{9}{10}, \frac{10}{10}

Let's put them in their lowest term:

110,15,310,25,12,35,710,45,910,11\displaystyle \frac{1}{10}, \frac{1}{5}, \frac{3}{10}, \frac{2}{5}, \frac{1}{2}, \frac{3}{5}, \frac{7}{10}, \frac{4}{5}, \frac{9}{10}, \frac{1}{1}

Notice that for the denominator 1010 there are exactly ϕ(10)=4\phi(10) = 4 fractions, e.g.:

110,310,710,910\displaystyle \frac{1}{10}, \frac{3}{10}, \frac{7}{10}, \frac{9}{10}

More generally, for each denominator xx we will have there are ϕ(x)\phi(x) such fractions in the list above.

Thus the set of 1010 fractions is split into subsets of size ϕ(d)\phi(d) for each dd dividing 1010.

A similar argument applies for any nn.

Formula

For a positive integer nn let p1,p2,,pkp_1, p_2, \dots, p_k be the distinct primes that divides nn, then we will have:

ϕ(n)=n(11p1)(11p2)(11pk)\displaystyle \phi(n) = n \left(1 - \frac{1}{p_1} \right) \left(1 - \frac{1}{p_2} \right) \cdots \left(1 - \frac{1}{p_k} \right)

For example, we have for n=10n=10 the primes that divides nn are 22 and 55 hence:

ϕ(10)=10(112)(115)=4\displaystyle \phi(10) = 10 \left(1 - \frac{1}{2} \right) \left(1 - \frac{1}{5} \right) = 4

For the proof of this formula, we need to know some important facts

Phi is a multiplicative function

Let nn and mm be a coprime positive integer then we will have:

ϕ(nm)=ϕ(n)ϕ(m)\phi(nm) = \phi(n) \phi(m)

Proof for this fact is: Let A,B,CA, B, C be the set of integers which are coprime to nn, mm, and nmnm respectively.

That is ϕ(n)=A\phi(n) = |A|, ϕ(m)=B\phi(m) = |B|, and ϕ(nm)=C\phi(nm) = |C|.

Then we need to proof that there is a bijection between the set A×BA \times B and the set CC.

In simpler terms, we need to proof that each element in A×BA \times B corresponds uniquely to an element in set CC, 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, nn and mm), then there exists a unique solution modulo the product of the modulo (nmnm).

If aa is coprime to nn and bb is coprime to mm this bijection sends (a,b)(a,b) to cc with cc coprime to nmnm (Because cc has no divisor in common with nn or mm).

Therefore there is a bijection between A×BA \times B with CC

Thus having: ϕ(nm)=A×B=ϕ(n)ϕ(m)\phi(nm) = |A| \times |B| = \phi(n) \phi(m) _{\Box}

Computing Phi with a prime power argument

Let pp be a prime number and k1k \geq 1, then we will have:

ϕ(pk)=pkpk1=pk(11p)\displaystyle \phi(p^k) = p^k - p^{k-1} = p^k \left(1 - \frac{1}{p}\right)

The proof for this is quite simple, the only way a positive integer xpkx \leq p^k having gcd(p,x)=1\mathrm{gcd}(p, x) = 1 is that pp does not divides xx.

Therefore xx would not be in the set {p,2p,,pk1p}\{p, 2p, \dots, p^{k-1}p\} which have pk1p^{k-1} numbers.

So ϕ(pk)=pkpk1\phi(p^k) = p^k - p^{k-1}. _{\Box}

Proof for Euler Totient's Function Formula

Now that we now some important facts, we can prove the Euler Totient's Function Formula.

Let nn be a positive integer, then we can represent nn using prime factorization as follows:

n=p1a1×p2a2××pkakn = p_1^{a_1} \times p_2^{a_2} \times \cdots \times p_k^{a_k}

Where pip_i is prime and aia_i is a positive integer for 1ik1 \leq i \leq k.

We can rewrite it as:

n=i=1kpiai\displaystyle n = \prod_{i=1}^{k} p_i^{a_i}

Using the facts of multiplicative function and phi with a prime power argument, we get:

ϕ(n)=ϕ(i=1kpiai)=i=1kϕ(piai)=i=1k(piai(11pi))=(i=1kpiai)(i=1k(11pi))=n(i=1k(11pi))=n(11p1)(11p2)(11pk)\begin{aligned} \phi(n) &= \phi\left(\prod_{i=1}^{k} p_i^{a_i}\right) \\ &= \prod_{i=1}^{k} \phi(p_i^{a_i}) \\ &= \prod_{i=1}^{k} \left( p_i^{a_i} \left(1 - \frac{1}{p_i} \right) \right) \\ &= \left( \prod_{i=1}^{k} p_i^{a_i} \right) \left(\prod_{i=1}^{k} \left(1 - \frac{1}{p_i} \right) \right) \\ &= n \left(\prod_{i=1}^{k} \left(1 - \frac{1}{p_i} \right) \right) \\ &= n \left(1 - \frac{1}{p_1} \right) \left(1 - \frac{1}{p_2} \right) \cdots \left(1 - \frac{1}{p_k} \right) _{\Box} \end{aligned}

Bonus (Code)

Here are C++ implementations for computing Euler's Totient Function:

euler-totient-function.cpp
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:

precompute-phi.cpp
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;
        }
    }
}

Footnotes

  1. Long, Calvin T. (1972), Elementary Introduction to Number Theory (2nd ed.), Lexington: D. C. Heath and Company, LCCN 77-171950

  2. Gauss, DA, art 39

  3. Graham et al. pp. 134-135