Euler's totient function is also known as the phi function
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
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.
If the input,
The input,
If the input,
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,
If the input,
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,
If the input,
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
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.
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;}
The explanation of the code above is as follows:
Line 9–12: The if
condition ensures that the larger number is in the variable, n
, and the second largest is in the variable, k
.
Line 14–20: The for
loop divides k
and n
with i
to check if both the variables are divisible by i
. The if
condition updates the value of the variable gcdValue
if i
is a common divisor of k
and n
. At the end of the for
loop, gcdValue
contains the greatest common divisor of variables k
and n
.
Line 30–37: The for
loop calls the function gcd()
with i
and n
as input and the result is stored in the variable gcdValue
. The if
condition checks if the value of the variable gcdValue
is equal to 1. If it is, the value of the variable, result
, is increased by 1.
Free Resources