Euler's Totient Function
Learn via video course
Overview
Totient function (denoted by ), also known as phi-function or Euler's Totient function, is a mathematical function which counts the number of integers in the range (both inclusive) that are co-prime to .
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 -
What is Euler's Totient Function?
Suppose your friend wants to challenge you for an ice cream treat. They give you a number , and they want you to find a number such that the greatest common divisor (GCD) of and is 1.
This is a fairly trivial task for you since you could say any prime number smaller than 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 lying between and 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 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 as input and outputs the number of integers present between and that are co-prime to .
Note:
- 2 positive integers a and b are said to be co-prime if their greatest common factor/divisor is equal to 1, that is,
- is considered co-prime to all numbers.
Example of Euler's Totient Function
Example 1
For an small example, let's take .
The numbers less than 10 are as follows:
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
Therefore,
Example 2
Let's take a bigger n, say 24. Factorizing it into pairs, we get:
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:
How to Compute Φ(n) for an Input n?
Having seen some examples, let's see how to calculate for any arbitrary . 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 . Using the fundamental theorem of arithmetic, we know that can be decomposed as a product of the positive integral powers of its prime factors. Therefore,
where are prime factors of .
Now using an interesting property of the totient function, which states that
provided that are co-prime to each other.
Using and , we get
Another interesting property of the totient function is that if is a power of a prime number, say where , then,
Using and , we get
Taking common from term from RHS of , we get
Hence we get
The above equation gives us a method to calculate for any desired .
Code Implementation
Using the result we derived above, it is quite easy to write a program to calculate for an input .
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 with all possible primes upto , and if that prime divides , then divide with that prime until it is no longer divisible by it.
- Further, we multiply the answer with . 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 is still not , then it must be the last prime factor of the original . (It is due to the fact that there can be atmost prime factor of which is greater than ). Thus, we include this last prime factor in our answer too.
Time Complexity: Space Complexity:
C++ Implementation 2
Explanation:
- This implementation not only calculates the value of for just , but also for all integers less than .
- Factorizing all the numbers from to 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 is subtracted from them. This is because phi[j]/i is equal to when phi[j] is equal to i. This is in accordance with the property 1 discussed below.
Time Complexity: Space Complexity:
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 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] ( 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 value of all numbers from to
Time Complexity: Space Complexity:
Properties of Euler's Totient Function
In addition to the above-used properties, we also have the following results related to the Totient function:
-
If is a prime number, then
-
If is a prime number and is a positive integer (that is, ), then
-
For any two positive integers and we have
Considering the case when and are coprime, then . In such a scenario, the above relation reduces to
-
The sum of values of the totient function over the positive divisors of equals itself. In other words:
-
If and are two prime numbers, then using (1) and (3), we get:
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 , are relatively prime, then
The converse of the Euler's theorem also holds, which is stated as:
If , then and are relatively prime.
A special case of this theorem where 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
Taking an example, suppose and . Using the methods described above, we get .
Therefore, .
Applying the operator on , we get
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 which is not co-prime to 10 (since their greatest common divisor is 2).
In this case, .
Therefore, .
Applying the operator on , we get
Thus, we've verified that the theorem does not hold when and are not co-prime.
Conclusion
-
Euler's totient function, , counts the number of integers between and (both inclusive) that are co-prime to .
-
can be calculated using the formula
where are prime factors on the input number .
-
If is a prime number, then
-
If is a prime number and is a positive integer (that is, ), then
-
For any two positive integers and we have
Considering the case when and are coprime, then . In such a scenario, the above relation reduces to
-
The sum of values of the totient function over the positive divisors of equals itself. In other words:
-
If and are two prime numbers, then using (1) and (3), we get:
This property is used in the RSA algorithm.
-
Euler's totient function is also used in Euler's theorem (also known as Euler's Totient Theorem) which states that:
-
Special case of Euler's theorem when is a prime number is called Fermat's Little Theorem which is used in the RSA encryption algorithm.
-
The value of Totient function for a number can be implemented in the best time complexity of with an associated space complexity of