What is Euler's totient function?
Euler's totient function is also known as the phi function
How does it work?
The totient function calculates the greatest common divisor of the integers
To understand the working of the totient function in more detail, we take an example of
Properties of Euler's totient function
The properties of Euler’s totient function make it easier to solve for the output a little easier and computationally less expensive, given that certain conditions are met. The properties of Euler’s totient function are as follows.
1.Input is a prime number
If the input,
Example
The input,
2.Input is in the form of
If the input,
Example
The input ,
Simplifying the equation gives us the following result.
This example shows the proof of the property of an Euler's totient function where the input,
3.Input is in the form of
If the input,
Example
The input,
Simplifying the equation gives us the following result:
This example shows the proof of the property of an Euler's totient function where the input,
4.Input is in the form of
If the input,
Example
The input,
Applying the first property on
Simplifying the equation gives us the following result.
This example shows the proof of the property of an Euler's totient function where the input
Applications of Euler's totient function
Euler's totient functions serve as the basis for modern-era cryptography. It is also useful in other places in number systems where the goal is to find the count of the co-prime number of a number. The applications of the function are as follows.
Euler's theorem: Euler's theorem uses Euler's totient function to extend the functionality of Fermat's little theorem, as Euler's theorem is valid for all positive integer values.
RSA encryption: The totient function is used in conjunction with Euler's theorem in RSA for the process of key generation, encryption, and decryption.
Code example
The code below shows how Euler's totient function values are calculated. Change the value of the variable, a, to change the input to the totient function.
#include <iostream>using namespace std;int gcd(int k, int n){int gcdValue = 1;// To select the largest numberif(k > n){swap(k, n);}for(int i = 1 ; i <= k; i++){ // To if "i" is the common divisor of two numberif(k % i == 0 && n % i == 0){gcdValue = i;}}return gcdValue;}int totient(int n){int result = 0;int gcdValue = 1;for(int i = 1 ; i <= n; i++){gcdValue = gcd(i, n);if(gcdValue == 1){result += 1;}}return result;}int main(){// Feel free to change aint a = 10;cout << "totient " << a << " : " << totient(a);return 0;}
Explanation
The explanation of the code above is as follows:
Line 9–12: The
ifcondition ensures that the larger number is in the variable,n, and the second largest is in the variable,k.Line 14–20: The
forloop divideskandnwithito check if both the variables are divisible byi. Theifcondition updates the value of the variablegcdValueifiis a common divisor ofkandn. At the end of theforloop,gcdValuecontains the greatest common divisor of variableskandn.Line 30–37: The
forloop calls the functiongcd()withiandnas input and the result is stored in the variablegcdValue. Theifcondition checks if the value of the variablegcdValueis equal to 1. If it is, the value of the variable,result, is increased by 1.
Free Resources