Euler's Totient Function

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
By Jitender Punia
Free
star4.9
Enrolled: 1000
DSA Problem Solving for Interviews using Java
DSA Problem Solving for Interviews using Java
Jitender Punia
Free
4.9
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

Totient function (denoted by ϕ:NN\phi:\mathbb{N} \rightarrow \mathbb{N} ), also known as phi-function or Euler's Totient function, is a mathematical function which counts the number of integers in the range [1,n][1, n] (both inclusive) that are co-prime to nn.

Scope Of Article

This article discusses Euler's totient function in data structure, its applications, examples, and code implementation, along with some interesting properties related to it.




Takeaways

  • Complexity of Totient function
    • Time complexity - O(nlog(n))\mathcal{O}(n\log{(n)})

What is Euler's Totient Function?

Suppose your friend wants to challenge you for an ice cream treat. They give you a number nn, and they want you to find a number mm such that the greatest common divisor (GCD) of nn and mm is 1.

This is a fairly trivial task for you since you could say any prime number smaller than nn and win the challenge. But you want to impress your friend a little, and therefore announce that you could tell them the exact number of all such mm lying between 11 and nn that satisfy the given condition.

Not believing you, your friend raises the stakes of the challenge, announcing that they will pay not only for ice cream but for the whole lunch you both just ate if you win.

Unlucky for your friend, you knew about the Euler's Totient Function, and since the given nn was quite small, you computed the answer in your mind and hence did not have to pay for the lunch.

Euler's totient function (also called phi-function or totient function) takes a single positive integer nn as input and outputs the number of integers present between 11 and nn that are co-prime to nn.

Note:

  1. 2 positive integers a and b are said to be co-prime if their greatest common factor/divisor is equal to 1, that is,
gcd(a,b)=1\gcd(a, b) = 1
  1. 11 is considered co-prime to all numbers.

Example of Euler's Totient Function

Example 1

For an small example, let's take n=10n = 10.

The numbers less than 10 are as follows:

1,2,3,4,5,6,7,8,91, 2, 3, 4, 5, 6, 7, 8, 9

Out of these,

  • 1 is co-prime to 10 (by definition).
  • 2 and 5 completely divide 10, therefore, are not co-prime to 10.
  • 4, 6, 8 are divisible by 2 (just like 10), therefore, their greatest common divisor is 2. Therefore, they are also not coprime to 10..
  • 3, 7, 9 neither divide 10 nor share any common factor with it. Therefore, by definition of coprime numbers, we saw earlier, these numbers are coprime to 10.

Therefore, there are 4 numbers less than 10 that are co-prime to it. These are

1,3,7,91, 3, 7, 9

Therefore,

ϕ(10)=4\phi(10) = 4

Example 2

Let's take a bigger n, say 24. Factorizing it into pairs, we get:

1×24=241 \times 24 = 24
2×12=242 \times 12 = 24
3×8=243 \times 8 = 24
4×6=244 \times 6 = 24

Now considering the numbers from 1 to 23:

  • 2, 3, 4, 6, 8, 12 completely divide 24 and therefore are not coprime to it.
  • 9, 10, 14, 15, 16, 18, 20, 21, 22 have a common factor with 24 which is greater than 1, therefore these numbers are also not coprime to 24
  • 1, 5, 7, 11, 13, 17, 19, 23 don't have any common factor other than 1 with 24, therefore, are co-prime to it.

The count of co-prime numbers to 24 (listed above) is 8. Therefore:

ϕ(n)=8\phi(n) = 8

How to Compute Φ(n) for an Input n?

Having seen some examples, let's see how to calculate ϕ(n)\phi(n) for any arbitrary nn. While describing the process, we might use some properties of the Totient function which you might not know. Don't worry about them, as we will see them later.

Suppose you're given an integer nNn \in \mathbb{N}. Using the fundamental theorem of arithmetic, we know that nn can be decomposed as a product of the positive integral powers of its prime factors. Therefore,

n=p1α1p2α2pkαk tag1n = {p_1^{\alpha_1}} {\cdot} {p_2^{\alpha_2}} \cdots p_k^{\alpha_k}\ tag{1}

where pip_i are prime factors of nn.

Now using an interesting property of the totient function, which states that

ϕ(abc)=ϕaϕ(b)ϕ(c) tag2\phi(abc) = \phi{a}\cdot\phi(b)\cdot\phi(c) \ tag{2}

provided that a,b,ca, b, c are co-prime to each other.

Using (1)(1) and (2)(2), we get

ϕ(n)=ϕ(p1α1p2α2pkαk)=ϕ(p1α1)ϕ(p2α2)ϕ(pkαk) tag3\phi(n) = \phi({p_1^{\alpha_1}} {\cdot} {p_2^{\alpha_2}} \cdots p_k^{\alpha_k}) = \phi(p_1^{\alpha_1})\cdot\phi(p_2^{\alpha_2})\cdots\phi(p_k^{\alpha_k}) \ tag{3}

Another interesting property of the totient function ϕ(m)\phi(m) is that if mm is a power of a prime number, say pαp^{\alpha} where α1\alpha \ge 1, then,

ϕ(m)=ϕ(pα)=pαpα1 tag4\phi(m) = \phi(p^{\alpha}) = p^{\alpha}-p^{\alpha-1} \ tag{4}

Using (4)(4) and (3)(3), we get

ϕ(n)=(p1α1p1α11)(p2α2p2α21)(p3α3p3α31)(pkαkpkαk1) tag5\phi(n) = (p_1^{\alpha_1}-p_1^{\alpha_1-1})\cdot(p_2^{\alpha_2}-p_2^{\alpha_2-1})\cdot(p_3^{\alpha_3}-p_3^{\alpha_3-1})\cdots(p_k^{\alpha_k}-p_k^{\alpha_k-1}) \ tag{5}

Taking piαip_i^{\alpha_i} common from ithi^{th} term from RHS of (5)(5), we get

KaTeX parse error: No such environment: align at position 7: \begin{̲a̲l̲i̲g̲n̲}̲ \phi (n) = p_1…
=p1α1p2α2pkαk(11p1)(11p2)(11pk)={p_1^{\alpha_1}} {\cdot} {p_2^{\alpha_2}} \cdots p_k^{\alpha_k}\cdot \left(1 - \frac{1}{p_1}\right) \cdot\left(1 - \frac{1}{p_2}\right) \cdots \left(1 - \frac{1}{p_k}\right)
=n(11p1)(11p2)(11pk)= n \cdot \left(1 - \frac{1}{p_1}\right) \cdot \left(1 - \frac{1}{p_2}\right) \cdots \left(1 - \frac{1}{p_k}\right)

Hence we get

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

The above equation gives us a method to calculate ϕ(n)\phi(n) for any desired nn.

Code Implementation

Using the result we derived above, it is quite easy to write a program to calculate ϕ(n)\phi(n) for an input nn.

C++ Implementation 1

Explanation:

  • The above implementation makes use of the result we derived in the last section. We initialize the answer with n, and then compute the remaining product.
  • The for loop checks the divisibility of nn with all possible primes upto n\sqrt{n}, and if that prime divides nn, then divide with that prime until it is no longer divisible by it.
  • Further, we multiply the answer with (11p)\left(1-\frac{1}{p}\right). Please note that answer*(1-(1/p)) is mathematically equivalent to answer - (answer/p), but the latter part is not prone to precision errors that occur due to division of floating point numbers as being done in the former.
  • Finally, if nn is still not 11, then it must be the last prime factor of the original nn. (It is due to the fact that there can be atmost 11 prime factor of mm which is greater than m\sqrt{m}). Thus, we include this last prime factor in our answer too.

Time Complexity: O(n)\mathcal{O}({\sqrt{n}}) Space Complexity: O(1)\mathcal{O}(1)


C++ Implementation 2

Explanation:

  • This implementation not only calculates the value of ϕ\phi for just nn, but also for all integers less than nn.
  • Factorizing all the numbers from 11 to nn and then using the formula for each such number would be highly inefficient.
  • Therefore, we use the idea of Sieve Of Erastothenes here. For each prime number in the list/array (checked by the clause phi[i]==i), we update all its multiples (present at phi[j]) by subtracting phi[j]/i from it.
  • For all prime numbers themselves, only 11 is subtracted from them. This is because phi[j]/i is equal to 11 when phi[j] is equal to i. This is in accordance with the property 1 discussed below.

Time Complexity: O(nlog(logn))\mathcal{O}(n\log(\log{n})) Space Complexity: O(n)\mathcal{O}(n)


C++ Implementation 3 (Using Divisor Sum Property)

Explanation:

  • This implementation uses the divisor sum property of the totient function (see property 4 below).
  • Initially, we subtract ϕ(1)\phi(1) from all numbers since 1 divides all numbers, and therefore, we initialize the ith element of phi vector with i-1.
  • Next, when we are at element i, we've already calculated phi[i] and the phi of all previous elements, and we will not update them any further.
  • Now we subtract the value of phi[i] (ϕ\phi of i) from all the multiples of i starting from 2*i which are less than n.
  • When we've calculated uptil n, then our array phi contains the ϕ\phi value of all numbers from 11 to nn

Time Complexity: O(nlog(n))\mathcal{O}(n\log{(n)}) Space Complexity: O(n)\mathcal{O}(n)

Properties of Euler's Totient Function

In addition to the above-used properties, we also have the following results related to the Totient function:

  1. If pp is a prime number, then

    ϕ(p)=p1\phi(p) = p-1
  2. If pp is a prime number and mm is a positive integer (that is, m1m \ge 1), then

    ϕ(pm)=pmpm1\phi(p^m) = p^{m}-p^{m-1}
  3. For any two positive integers mm and nn we have

    ϕ(mn)=ϕ(m)ϕ(n)gcd(m,n)ϕ(gcd(m,n))\phi(mn) = \phi(m)\cdot\phi(n)\cdot \frac{\gcd(m, n)}{\phi(\gcd(m, n))}

    Considering the case when mm and nn are coprime, then gcd(m,n)=1\gcd(m,n)=1. In such a scenario, the above relation reduces to

    ϕ(mn)=ϕ(m)ϕ(n)\phi(mn) = \phi(m)\cdot\phi(n)
  4. The sum of values of the totient function over the positive divisors of nn equals nn itself. In other words:

    knϕ(k)=n\sum_{k|n} \phi{(k)} = n
  5. If mm and nn are two prime numbers, then using (1) and (3), we get:

    ϕ(mn)=ϕ(m)ϕ(n)=(m1)(n1)\phi(mn) = \phi(m)\cdot\phi(n) = (m-1)\cdot(n-1)

    This property is used in the RSA algorithm.

Application of Totient Function in Euler's Theorem

Euler's theorem (sometimes also called as Euler's totient theorem) which makes use of Euler's totient function, states the following:

If aa, nn N\in \mathbb{N} are relatively prime, then

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

The converse of the Euler's theorem also holds, which is stated as:

If aϕ(n)1modna^{\phi(n)} \equiv 1 \mod{n}, then aa and nn are relatively prime.

A special case of this theorem where nn is a prime number is used in the RSA encryption algorithm. This special case of the theorem is popularly known as Fermat's Little Theorem and states that

an11modna^{n-1} \equiv 1 \mod{n}


Taking an example, suppose a=10a = 10 and n=9n = 9. Using the methods described above, we get ϕ(9)=6\phi(9) = 6.

Therefore, aϕ(n)=106a^{\phi(n)} = 10^{6}.

Applying the modn (=mod9)\mod n \ (=\mod 9) operator on 10610^6, we get

106mod9=(11111×9+1)mod9=0+(1mod9)=110^6\mod9= (11111\times9+1)\mod9 = 0 + (1\mod9) = 1

which was the expected result since 10 and 9 don't have any common divisor other than 1, that is to say, they are co-prime.


Suppose we had taken a number n=2n = 2 which is not co-prime to 10 (since their greatest common divisor is 2).

In this case, ϕ(n)=ϕ(2)=1\phi(n)= \phi(2) =1.

Therefore, aϕ(n)=101=10a^{\phi(n)} = 10^{1} = 10.

Applying the modn (=mod2)\mod n \ (=\mod 2) operator on 1010, we get

10mod2=(5×2)mod2=0110\mod2= (5\times2)\mod2 = 0 \neq 1

Thus, we've verified that the theorem does not hold when aa and nn are not co-prime.

Conclusion

  1. Euler's totient function, ϕ:NN\phi:\mathbb{N} \rightarrow \mathbb{N}, counts the number of integers between 11 and nn (both inclusive) that are co-prime to nn.

  2. ϕ(n)\phi(n) can be calculated using the formula

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

    where pip_i are prime factors on the input number nn.

  3. If pp is a prime number, then

    ϕ(p)=p1\phi(p) = p-1
  4. If pp is a prime number and mm is a positive integer (that is, m1m \ge 1), then

    ϕ(pm)=pmpm1\phi(p^m) = p^{m}-p^{m-1}
  5. For any two positive integers mm and nn we have

    ϕ(mn)=ϕ(m)ϕ(n)gcd(m,n)ϕ(gcd(m,n))\phi(mn) = \phi(m)\cdot\phi(n)\cdot \frac{\gcd(m, n)}{\phi(\gcd(m, n))}

    Considering the case when mm and nn are coprime, then gcd(m,n)=1\gcd(m,n)=1. In such a scenario, the above relation reduces to

    ϕ(mn)=ϕ(m)ϕ(n)\phi(mn) = \phi(m)\cdot\phi(n)
  6. The sum of values of the totient function over the positive divisors of nn equals nn itself. In other words:

    knϕ(k)=n\sum_{k|n} \phi{(k)} = n
  7. If mm and nn are two prime numbers, then using (1) and (3), we get:

    ϕ(mn)=ϕ(m)ϕ(n)=(m1)(n1)\phi(mn) = \phi(m)\cdot\phi(n) = (m-1)\cdot(n-1)

    This property is used in the RSA algorithm.

  8. Euler's totient function is also used in Euler's theorem (also known as Euler's Totient Theorem) which states that:

aϕ(n)1modna,n are relatively primea^{\phi(n)} \equiv 1\mod{n} \iff a, n\ \text{are relatively prime}
  1. Special case of Euler's theorem when nn is a prime number is called Fermat's Little Theorem which is used in the RSA encryption algorithm.

  2. The value of Totient function for a number nn can be implemented in the best time complexity of O(nlog(logn))\mathcal{O}(n\log{(\log{n})}) with an associated space complexity of O(n)\mathcal{O}(n)