The Wayback Machine - https://web.archive.org/web/20240823032939/https://www.geeksforgeeks.org/eulers-totient-function/
Open In App

Euler’s Totient Function

Last Updated : 24 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Euler’s Totient function Φ(n) for an input n is the count of numbers in {1, 2, 3, …, n-1} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1.

Examples :

Φ(1) = 1
gcd(1, 1) is 1

Φ(2) = 1
gcd(1, 2) is 1, but gcd(2, 2) is 2.

Φ(3) = 2
gcd(1, 3) is 1 and gcd(2, 3) is 1

Φ(4) = 2
gcd(1, 4) is 1 and gcd(3, 4) is 1

Φ(5) = 4
gcd(1, 5) is 1, gcd(2, 5) is 1,
gcd(3, 5) is 1 and gcd(4, 5) is 1

Φ(6) = 2
gcd(1, 6) is 1 and gcd(5, 6) is 1,

Recommended Practice

How to compute Φ(n) for an input n?
A simple solution is to iterate through all numbers from 1 to n-1 and count numbers with gcd with n as 1. Below is the implementation of the simple method to compute Euler’s Totient function for an input integer n. 

C

// A simple C program to calculate Euler's Totient Function #include <stdio.h> // Function to return gcd of a and b int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // A simple method to evaluate Euler Totient Function int phi(unsigned int n) { unsigned int result = 1; for (int i = 2; i < n; i++) if (gcd(i, n) == 1) result++; return result; } // Driver program to test above function int main() { int n; for (n = 1; n <= 10; n++) printf("phi(%d) = %d\n", n, phi(n)); return 0; }

Java

// A simple java program to calculate // Euler's Totient Function import java.io.*; class GFG { // Function to return GCD of a and b static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // A simple method to evaluate // Euler Totient Function static int phi(int n) { int result = 1; for (int i = 2; i < n; i++) if (gcd(i, n) == 1) result++; return result; } // Driver code public static void main(String[] args) { int n; for (n = 1; n <= 10; n++) System.out.println("phi(" + n + ") = " + phi(n)); } } // This code is contributed by sunnusingh

Python3

# A simple Python3 program # to calculate Euler's # Totient Function # Function to return # gcd of a and b def gcd(a, b): if (a == 0): return b return gcd(b % a, a) # A simple method to evaluate # Euler Totient Function def phi(n): result = 1 for i in range(2, n): if (gcd(i, n) == 1): result+=1 return result # Driver Code for n in range(1, 11): print("phi(",n,") = ", phi(n), sep = "") # This code is contributed # by Smitha

C#

// A simple C# program to calculate // Euler's Totient Function using System; class GFG { // Function to return GCD of a and b static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // A simple method to evaluate // Euler Totient Function static int phi(int n) { int result = 1; for (int i = 2; i < n; i++) if (gcd(i, n) == 1) result++; return result; } // Driver code public static void Main() { for (int n = 1; n <= 10; n++) Console.WriteLine("phi(" + n + ") = " + phi(n)); } } // This code is contributed by nitin mittal

JavaScript

<script> // Javascript program to calculate // Euler's Totient Function // Function to return // gcd of a and b function gcd(a, b) { if (a == 0) return b; return gcd(b % a, a); } // A simple method to evaluate // Euler Totient Function function phi(n) { let result = 1; for (let i = 2; i < n; i++) if (gcd(i, n) == 1) result++; return result; } // Driver Code for (let n = 1; n <= 10; n++) document.write(`phi(${n}) = ${phi(n)} <br>`); // This code is contributed by _saurabh_jaiswal </script>

PHP

<&Phi;php // PHP program to calculate // Euler's Totient Function // Function to return // gcd of a and b function gcd($a, $b) { if ($a == 0) return $b; return gcd($b % $a, $a); } // A simple method to evaluate // Euler Totient Function function phi($n) { $result = 1; for ($i = 2; $i < $n; $i++) if (gcd($i, $n) == 1) $result++; return $result; } // Driver Code for ($n = 1; $n <= 10; $n++) echo "phi(" .$n. ") =" . phi($n)."\n"; // This code is contributed by Sam007 &Phi;>

C++

// A simple C++ program to calculate // Euler's Totient Function #include <iostream> using namespace std; // Function to return gcd of a and b int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // A simple method to evaluate Euler Totient Function int phi(unsigned int n) { unsigned int result = 1; for (int i = 2; i < n; i++) if (gcd(i, n) == 1) result++; return result; } // Driver program to test above function int main() { int n; for (n = 1; n <= 10; n++) cout << "phi("<<n<<") = " << phi(n) << endl; return 0; } // This code is contributed by SHUBHAMSINGH10


Output

phi(1) = 1 phi(2) = 1 phi(3) = 2 phi(4) = 2 phi(5) = 4 phi(6) = 2 phi(7) = 6 phi(8) = 4 phi(9) = 6 phi(10) = 4


The above code calls gcd function O(n) times. The time complexity of the gcd function is O(h) where “h” is the number of digits in a smaller number of given two numbers. Therefore, an upper bound on the time complexity of the above solution is O(N^2 log N) [How Φ there can be at most Log10n digits in all numbers from 1 to n]

Auxiliary Space: O(log N)


Below is a Better Solution. The idea is based on Euler’s product formula which states that the value of totient functions is below the product overall prime factors p of n. 

Euler's-Product-Formula

The formula basically says that the value of Φ(n) is equal to n multiplied by-product of (1 – 1/p) for all prime factors p of n. For example value of Φ(6) = 6 * (1-1/2) * (1 – 1/3) = 2.
We can find all prime factors using the idea used in this post. 

1) Initialize : result = n
2) Run a loop from 'p' = 2 to sqrt(n), do following for every 'p'.
a) If p divides n, then
Set: result = result * (1.0 - (1.0 / (float) p));
Divide all occurrences of p in n.
3) Return result


Below is the implementation of Euler’s product formula.  

C++

// C++ program to calculate Euler's // Totient Function using Euler's // product formula #include <bits/stdc++.h> using namespace std; int phi(int n) { // Initialize result as n float result = n; // Consider all prime factors of n // and for every prime factor p, // multiply result with (1 - 1/p) for(int p = 2; p * p <= n; ++p) { // Check if p is a prime factor. if (n % p == 0) { // If yes, then update n and result while (n % p == 0) n /= p; result *= (1.0 - (1.0 / (float)p)); } } // If n has a prime factor greater than sqrt(n) // (There can be at-most one such prime factor) if (n > 1) result -= result / n; //Since in the set {1,2,....,n-1}, all numbers are relatively prime with n //if n is a prime number return (int)result; } // Driver code int main() { int n; for(n = 1; n <= 10; n++) { cout << "Phi" << "(" << n << ")" << " = " << phi(n) <<endl; } return 0; } // This code is contributed by koulick_sadhu

C

// C program to calculate Euler's Totient Function // using Euler's product formula #include <stdio.h> int phi(int n) { float result = n; // Initialize result as n // Consider all prime factors of n and for every prime // factor p, multiply result with (1 - 1/p) for (int p = 2; p * p <= n; ++p) { // Check if p is a prime factor. if (n % p == 0) { // If yes, then update n and result while (n % p == 0) n /= p; result *= (1.0 - (1.0 / (float)p)); } } // If n has a prime factor greater than sqrt(n) // (There can be at-most one such prime factor) if (n > 1) result -= result / n; //Since in the set {1,2,....,n-1}, all numbers are relatively prime with n //if n is a prime number return (int)result; } // Driver program to test above function int main() { int n; for (n = 1; n <= 10; n++) printf("phi(%d) = %d\n", n, phi(n)); return 0; }

Java

// Java program to calculate Euler's Totient // Function using Euler's product formula import java.io.*; class GFG { static int phi(int n) { // Initialize result as n float result = n; // Consider all prime factors of n and for // every prime factor p, multiply result // with (1 - 1/p) for (int p = 2; p * p <= n; ++p) { // Check if p is a prime factor. if (n % p == 0) { // If yes, then update n and result while (n % p == 0) n /= p; result *= (1.0 - (1.0 / (float)p)); } } // If n has a prime factor greater than sqrt(n) // (There can be at-most one such prime factor) if (n > 1) result -= result / n; //Since in the set {1,2,....,n-1}, all numbers are relatively prime with n //if n is a prime number return (int)result; } // Driver program to test above function public static void main(String args[]) { int n; for (n = 1; n <= 10; n++) System.out.println("phi(" + n + ") = " + phi(n)); } } // This code is contributed by Nikita Tiwari.

Python3

# Python 3 program to calculate # Euler's Totient Function # using Euler's product formula def phi(n) : result = n # Initialize result as n # Consider all prime factors # of n and for every prime # factor p, multiply result with (1 - 1 / p) p = 2 while p * p<= n : # Check if p is a prime factor. if n % p == 0 : # If yes, then update n and result while n % p == 0 : n = n // p result = result * (1.0 - (1.0 / float(p))) p = p + 1 # If n has a prime factor # greater than sqrt(n) # (There can be at-most one # such prime factor) if n > 1 : result -= result // n #Since in the set {1,2,....,n-1}, all numbers are relatively prime with n #if n is a prime number return int(result) # Driver program to test above function for n in range(1, 11) : print("phi(", n, ") = ", phi(n)) # This code is contributed # by Nikita Tiwari.

C#

// C# program to calculate Euler's Totient // Function using Euler's product formula using System; class GFG { static int phi(int n) { // Initialize result as n float result = n; // Consider all prime factors // of n and for every prime // factor p, multiply result // with (1 - 1 / p) for (int p = 2; p * p <= n; ++p) { // Check if p is a prime factor. if (n % p == 0) { // If yes, then update // n and result while (n % p == 0) n /= p; result *= (float)(1.0 - (1.0 / (float)p)); } } // If n has a prime factor // greater than sqrt(n) // (There can be at-most // one such prime factor) if (n > 1) result -= result / n; //Since in the set {1,2,....,n-1}, all numbers are relatively prime with n //if n is a prime number return (int)result; } // Driver Code public static void Main() { int n; for (n = 1; n <= 10; n++) Console.WriteLine("phi(" + n + ") = " + phi(n)); } } // This code is contributed by nitin mittal.

JavaScript

// Javascript program to calculate // Euler's Totient Function // using Euler's product formula function phi(n) { // Initialize result as n let result = n; // Consider all prime factors // of n and for every prime // factor p, multiply result // with (1 - 1/p) for (let p = 2; p * p <= n; ++p) { // Check if p is // a prime factor. if (n % p == 0) { // If yes, then update // n and result while (n % p == 0) n /= p; result *= (1.0 - (1.0 / p)); } } // If n has a prime factor greater // than sqrt(n) (There can be at-most // one such prime factor) if (n > 1) result -= result / n; //Since in the set {1,2,....,n-1}, all numbers are relatively prime with n //if n is a prime number return parseInt(result); } // Driver Code for (let n = 1; n <= 10; n++) document.write(`phi(${n}) = ${phi(n)} <br>`); // This code is contributed by _saurabh_jaiswal

PHP

<&Phi;php // PHP program to calculate // Euler's Totient Function // using Euler's product formula function phi($n) { // Initialize result as n $result = $n; // Consider all prime factors // of n and for every prime // factor p, multiply result // with (1 - 1/p) for ($p = 2; $p * $p <= $n; ++$p) { // Check if p is // a prime factor. if ($n % $p == 0) { // If yes, then update // n and result while ($n % $p == 0) $n /= $p; $result *= (1.0 - (1.0 / $p)); } } // If n has a prime factor greater // than sqrt(n) (There can be at-most // one such prime factor) if ($n > 1) $result -= $result / $n; //Since in the set {1,2,....,n-1}, all numbers are relatively prime with n //if n is a prime number return intval($result); } // Driver Code for ($n = 1; $n <= 10; $n++) echo "phi(" .$n. ") =" . phi($n)."\n"; // This code is contributed by Sam007 &Phi;>


Output

Phi(1) = 1 Phi(2) = 1 Phi(3) = 2 Phi(4) = 2 Phi(5) = 4 Phi(6) = 2 Phi(7) = 6 Phi(8) = 4 Phi(9) = 6 Phi(10) = 4

Time Complexity: O(Φ n log n)
Auxiliary Space: O(1)

We can avoid floating-point calculations in the above method. The idea is to count all prime factors and their multiples and subtract this count from n to get the totient function value (Prime factors and multiples of prime factors won’t have gcd as 1) 

1) Initialize result as n
2) Consider every number 'p' (where 'p' varies from 2 to Φ(n)).
If p divides n, then do following
a) Subtract all multiples of p from 1 to n [all multiples of p
will have gcd more than 1 (at least p) with n]
b) Update n by repeatedly dividing it by p.
3) If the reduced n is more than 1, then remove all multiples
of n from result.

Below is the implementation of the above algorithm. 

C++

// C++ program to calculate Euler's // Totient Function #include <bits/stdc++.h> using namespace std; int phi(int n) { // Initialize result as n int result = n; // Consider all prime factors of n // and subtract their multiples // from result for(int p = 2; p * p <= n; ++p) { // Check if p is a prime factor. if (n % p == 0) { // If yes, then update n and result while (n % p == 0) n /= p; result -= result / p; } } // If n has a prime factor greater than sqrt(n) // (There can be at-most one such prime factor) if (n > 1) result -= result / n; return result; } // Driver code int main() { int n; for(n = 1; n <= 10; n++) { cout << "Phi" << "(" << n << ")" << " = " << phi(n) << endl; } return 0; } // This code is contributed by koulick_sadhu

C

// C program to calculate Euler's Totient Function #include <stdio.h> int phi(int n) { int result = n; // Initialize result as n // Consider all prime factors of n and subtract their // multiples from result for (int p = 2; p * p <= n; ++p) { // Check if p is a prime factor. if (n % p == 0) { // If yes, then update n and result while (n % p == 0) n /= p; result -= result / p; } } // If n has a prime factor greater than sqrt(n) // (There can be at-most one such prime factor) if (n > 1) result -= result / n; return result; } // Driver program to test above function int main() { int n; for (n = 1; n <= 10; n++) printf("phi(%d) = %d\n", n, phi(n)); return 0; }

Java

// Java program to calculate // Euler's Totient Function import java.io.*; class GFG { static int phi(int n) { // Initialize result as n int result = n; // Consider all prime factors // of n and subtract their // multiples from result for (int p = 2; p * p <= n; ++p) { // Check if p is // a prime factor. if (n % p == 0) { // If yes, then update // n and result while (n % p == 0) n /= p; result -= result / p; } } // If n has a prime factor // greater than sqrt(n) // (There can be at-most // one such prime factor) if (n > 1) result -= result / n; return result; } // Driver Code public static void main (String[] args) { int n; for (n = 1; n <= 10; n++) System.out.println("phi(" + n + ") = " + phi(n)); } } // This code is contributed by ajit

Python3

# Python3 program to calculate # Euler's Totient Function def phi(n): # Initialize result as n result = n; # Consider all prime factors # of n and subtract their # multiples from result p = 2; while(p * p <= n): # Check if p is a # prime factor. if (n % p == 0): # If yes, then # update n and result while (n % p == 0): n = int(n / p); result -= int(result / p); p += 1; # If n has a prime factor # greater than sqrt(n) # (There can be at-most # one such prime factor) if (n > 1): result -= int(result / n); return result; # Driver Code for n in range(1, 11): print("phi(",n,") =", phi(n)); # This code is contributed # by mits

C#

// C# program to calculate // Euler's Totient Function using System; class GFG { static int phi(int n) { // Initialize result as n int result = n; // Consider all prime // factors of n and // subtract their // multiples from result for (int p = 2; p * p <= n; ++p) { // Check if p is // a prime factor. if (n % p == 0) { // If yes, then update // n and result while (n % p == 0) n /= p; result -= result / p; } } // If n has a prime factor // greater than sqrt(n) // (There can be at-most // one such prime factor) if (n > 1) result -= result / n; return result; } // Driver Code static public void Main () { int n; for (n = 1; n <= 10; n++) Console.WriteLine("phi(" + n + ") = " + phi(n)); } } // This code is contributed // by akt_mit

JavaScript

// Javascript program to calculate // Euler's Totient Function function phi(n) { // Initialize // result as n let result = n; // Consider all prime // factors of n and subtract // their multiples from result for (let p = 2; p * p <= n; ++p) { // Check if p is // a prime factor. if (n % p == 0) { // If yes, then // update n and result while (n % p == 0) n = parseInt(n / p); result -= parseInt(result / p); } } // If n has a prime factor // greater than sqrt(n) // (There can be at-most // one such prime factor) if (n > 1) result -= parseInt(result / n); return result; } // Driver Code for (let n = 1; n <= 10; n++) document.write(`phi(${n}) = ${phi(n)} <br>`); // This code is contributed // by _saurabh_jaiswal

PHP

<&Phi;php // PHP program to calculate // Euler's Totient Function function phi($n) { // Initialize // result as n $result = $n; // Consider all prime // factors of n and subtract // their multiples from result for ($p = 2; $p * $p <= $n; ++$p) { // Check if p is // a prime factor. if ($n % $p == 0) { // If yes, then // update n and result while ($n % $p == 0) $n = (int)$n / $p; $result -= (int)$result / $p; } } // If n has a prime factor // greater than sqrt(n) // (There can be at-most // one such prime factor) if ($n > 1) $result -= (int)$result / $n; return $result; } // Driver Code for ($n = 1; $n <= 10; $n++) echo "phi(", $n,") =", phi($n), "\n"; // This code is contributed // by ajit &Phi;>


Output

Phi(1) = 1 Phi(2) = 1 Phi(3) = 2 Phi(4) = 2 Phi(5) = 4 Phi(6) = 2 Phi(7) = 6 Phi(8) = 4 Phi(9) = 6 Phi(10) = 4

Time Complexity: O(Φ n log n)
Auxiliary Space: O(1)

Let us take an example to understand the above algorithm. 

n = 10.
Initialize: result = 10

2 is a prime factor, so n = n/i = 5, result = 5
3 is not a prime factor.

The for loop stops after 3 as 4*4 is not less than or equal
to 10.

After for loop, result = 5, n = 5
Since n > 1, result = result - result/n = 4

Some Interesting Properties of Euler’s Totient Function 


1) For a prime number p, [Tex]\phi(p) = p – 1[/Tex]

Proof :

[Tex]\phi(p) = p - 1[/Tex] , where p is any prime numberWe know that [Tex]gcd(p, k) = 1[/Tex] where k is any random number and [Tex]k \neq p[/Tex][Tex]\\[/Tex]Total number from 1 to p = p Number for which [Tex]gcd(p, k) = 1[/Tex] is [Tex]1[/Tex], i.e the number p itself, so subtracting 1 from p [Tex]\phi(p) = p - 1[/Tex]

Examples :  

[Tex]\phi(5) = 5 - 1 = 4[/Tex][Tex]\\[/Tex][Tex]\phi(13) = 13 - 1 = 12[/Tex][Tex]\\[/Tex][Tex]\phi(29) = 29 - 1 = 28[/Tex]


2) For two prime numbers a and b[Tex] \phi(a \cdot b) = \phi(a) \cdot \phi(b) = (a – 1) \cdot (b – 1)           [/Tex], used in RSA Algorithm

Proof :

[Tex]\phi(a\cdot b) = \phi(a) \cdot \phi(b)[/Tex], where a and b are prime numbers[Tex]\phi(a) = a - 1[/Tex] , [Tex]\phi(b) = b - 1[/Tex][Tex]\\[/Tex]Total number from 1 to ab = ab Total multiples of a from 1 to ab = [Tex]\frac{a \cdot b} {a}[/Tex] = [Tex]b[/Tex]Total multiples of b from 1 to ab = [Tex]\frac{a \cdot b} {b}[/Tex] = [Tex]a[/Tex]Example:a = 5, b = 7, ab = 35Multiples of a = [Tex]\frac {35} {5}[/Tex] = 7 {5, 10, 15, 20, 25, 30, 35}Multiples of b = [Tex]\frac {35} {7}[/Tex] = 5 {7, 14, 21, 28, 35}[Tex]\\[/Tex]Can there be any double counting ?(watch above example carefully, try with other prime numbers also for more grasp)Ofcourse, we have counted [Tex]ab[/Tex] twice in multiples of a and multiples of b so, Total multiples = a + b - 1 (with which [Tex]gcd \neq 1[/Tex] with [Tex]ab[/Tex])[Tex]\\[/Tex][Tex]\phi(ab) = ab - (a + b - 1)[/Tex] , removing all number with [Tex]gcd \neq 1[/Tex] with [Tex]ab[/Tex] [Tex]\phi(ab) = a(b - 1) - (b - 1)[/Tex][Tex]\phi(ab) = (a - 1) \cdot (b - 1)[/Tex][Tex]\phi(ab) = \phi(a) \cdot \phi(b)[/Tex]

Examples :

[Tex]\phi(5 \cdot 7) = \phi(5) \cdot \phi(7) = (5 - 1) \cdot (7 - 1) = 24[/Tex][Tex]\\[/Tex][Tex]\phi(3 \cdot 5) = \phi(3) \cdot \phi(5) = (3 - 1) \cdot (5 - 1) = 8[/Tex][Tex]\\[/Tex][Tex]\phi(3 \cdot 7) = \phi(3) \cdot \phi(7) = (3 - 1) \cdot (7 - 1) = 12[/Tex]


3) For a prime number p, [Tex]\phi(p ^ k) = p ^ k – p ^ {k – 1}[/Tex]

Proof : 

[Tex]\phi(p^k) = p ^ k - p ^{k - 1}[/Tex] , where p is a prime number[Tex]\\[/Tex]Total numbers from 1 to [Tex]p ^ k = p ^ k[/Tex] Total multiples of [Tex]p = \frac {p ^ k} {p} = p ^ {k - 1}[/Tex]Removing these multiples as with them [Tex]gcd \neq 1[/Tex][Tex]\\[/Tex]Example : p = 2, k = 5, [Tex]p ^ k[/Tex] = 32Multiples of 2 (as with them [Tex]gcd \neq 1[/Tex]) = 32 / 2 = 16 {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32}[Tex]\\[/Tex][Tex]\phi(p ^ k) = p ^ k - p ^ {k - 1}[/Tex]

Examples : 

[Tex]\phi(2 ^ 5) = 2 ^ 5 - 2 ^ {5 - 1} = 32 - 16 = 16[/Tex][Tex]\\[/Tex][Tex]\phi(5 ^ 3) = 5 ^ 3 - 5 ^ {3 - 1} = 125 - 25 = 100[/Tex][Tex]\\[/Tex][Tex]\phi(3 ^ 5) = 3 ^ 5 - 3 ^ {5 - 1} = 243 - 81 = 162[/Tex]


4) For two number a and b [Tex]\phi(a \cdot b)           [/Tex] [Tex]= \phi(a) \cdot \phi(b)           [/Tex] [Tex]\cdot \frac {gcd(a, b)} {\phi(gcd(a, b))}[/Tex]

Special Case : gcd(a, b) = 1

[Tex]\phi(a \cdot b) = \phi(a) \cdot \phi(b) \cdot \frac {1} {\phi(1)} = \phi(a) \cdot \phi(b)[/Tex]

Examples :

Special Case : [Tex]gcd(a, b) = 1[/Tex], [Tex]\phi(a \cdot b) = \phi(a) \cdot \phi(b)[/Tex] [Tex]\phi(2 \cdot 9) = \phi(2) \cdot \phi(9) = 1 \cdot 6 = 6[/Tex][Tex]\\[/Tex][Tex]\phi(8 \cdot 9) = \phi(8) \cdot \phi(9) = 4 \cdot 6 = 24[/Tex][Tex]\\[/Tex][Tex]\phi(5 \cdot 6) = \phi(5) \cdot \phi(6) = 4 \cdot 2 = 8[/Tex] [Tex]\\[/Tex][Tex]\\[/Tex]Normal Case : [Tex]gcd(a, b) \neq 1[/Tex], [Tex]\phi(a \cdot b) = \phi(a) \cdot \phi(b) \cdot \frac {gcd(a, b)} {\phi(gcd(a, b))}[/Tex][Tex]\\[/Tex][Tex]\phi(4 \cdot 6) = \phi(4) \cdot \phi(6) \cdot \frac {gcd(4, 6)} {\phi(gcd(4, 6))}[/Tex] [Tex]= 2 \cdot 2 \cdot \frac{2}{1}[/Tex] [Tex]= 2 \cdot 2 \cdot 2 = 8[/Tex][Tex]\\[/Tex][Tex]\phi(4 \cdot 8) = \phi(4) \cdot \phi(8) \cdot \frac {gcd(4, 8)} {\phi(gcd(4, 8))} = 2 \cdot 4 \cdot \frac{4}{2} = 2 \cdot 4 \cdot 2 = 16[/Tex][Tex]\\[/Tex][Tex]\phi(6 \cdot 8) = \phi(6) \cdot \phi(8) \cdot \frac {gcd(6, 8)} {\phi(gcd(6, 8))} = 2 \cdot 4 \cdot \frac{2}{1} = 2 \cdot 4 \cdot 2 = 16[/Tex]

5) Sum of values of totient functions of all divisors of n is equal to n. 
 

gausss


Examples :

n = 6
factors = {1, 2, 3, 6}
n = [Tex]\phi(1) + \phi(2) + \phi(3) + \phi(6)[/Tex] = 1 + 1 + 2 + 2 = 6[Tex]\\[/Tex]n = 8factors = {1, 2, 4, 8}n = [Tex]\phi(1) + \phi(2) + \phi(4) + \phi(8)[/Tex] = 1 + 1 + 2 + 4 = 8[Tex]\\[/Tex]n = 10factors = {1, 2, 5, 10}n = [Tex]\phi(1) + \phi(2) + \phi(5) + \phi(10)[/Tex] = 1 + 1 + 4 + 4 = 10

6) The most famous and important feature is expressed in Euler’s theorem : 

The theorem states that if n and a are coprime
(or relatively prime) positive integers, then

aΦ(n) Φ 1 (mod n)

The RSA cryptosystem is based on this theorem:
In the particular case when m is prime say p, Euler’s theorem turns into the so-called Fermat’s little theorem : 

ap-1 Φ 1 (mod p)

7) Number of generators of a finite cyclic group under modulo n addition is Φ(n).

Related Article: 
Euler’s Totient function for all numbers smaller than or equal to n 
Optimized Euler Totient Function for Multiple Evaluations

References: 
http://e-maxx.ru/algo/euler_function
http://en.wikipedia.org/wiki/Euler%27s_totient_function

https://cp-algorithms.com/algebra/phi-function.html

http://mathcenter.oxford.memory.edu/site/math125/chineseRemainderTheorem/
 



Previous Article
Next Article

Similar Reads

Probability of Euler's Totient Function in a range [L, R] to be divisible by M
Given three integers L, R, and M, the task is to find the probability of Euler's Totient Function of a number in the range [L, R] is divisible by M. Euler’s Totient function is the count of numbers in {1, 2, 3, …, N} that are relatively prime to N, i.e., the numbers whose GCD (Greatest Common Divisor) with N is 1. Examples: Input: L = 1, R = 5, M =
12 min read
Check if Euler Totient Function is same for a given number and twice of that number
Given an integer N, the task is to check whether the Euler’s Totient Function of N and 2 * N are the same or not. If they are found to be the same, then print “Yes”. Otherwise, print “No”. Examples: Input: N = 9 Output: Yes Explanation: Let phi() be the Euler Totient function Since 1, 2, 4, 5, 7, 8 have GCD 1 with 9. Therefore, phi(9) = 6. Since 1,
7 min read
Generate an array having sum of Euler Totient Function of all elements equal to N
Given a positive integer N, the task is to generate an array such that the sum of the Euler Totient Function of each element is equal to N. Examples: Input: N = 6Output: 1 6 2 3 Input: N = 12Output: 1 12 2 6 3 4 Approach: The given problem can be solved based on the divisor sum property of the Euler Totient Function, i.e., The Euler Totient Functio
5 min read
Euler's Totient function for all numbers smaller than or equal to n
Euler's Totient function ?(n) for an input n is the count of numbers in {1, 2, 3, ..., n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. For example, ?(4) = 2, ?(3) = 2 and ?(5) = 4. There are 2 numbers smaller or equal to 4 that are relatively prime to 4, 2 numbers smaller or equal to 3 that are
13 min read
Optimized Euler Totient Function for Multiple Evaluations
Euler Totient Function (ETF) ?(n) for an input n is count of numbers in {1, 2, 3, …, n} that are relatively prime to n, i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. Examples: ?(5) = 4 gcd(1, 5) is 1, gcd(2, 5) is 1, gcd(3, 5) is 1 and gcd(4, 5) is 1 ?(6) = 2 gcd(1, 6) is 1 and gcd(5, 6) is 1, Recommended PracticeEuler Totient
9 min read
Euler Totient Function in Python
Euler’s Totient function, also known as Euler's Phi Function Φ (n) is a mathematical function that counts the number of positive integers up to a given integer n that are relatively prime to n. i.e., the numbers whose GCD (Greatest Common Divisor) with n is 1. Examples : Input: Φ(1) = 1 Output: gcd(1, 1) is 1 Input: Φ(2) = 1Output: gcd(1, 2) is 1,
4 min read
Count integers in a range which are divisible by their euler totient value
Given 2 integers L and R, the task is to find out the number of integers in the range [L, R] such that they are completely divisible by their Euler totient value.Examples: Input: L = 2, R = 3 Output: 1 [Tex] [/Tex](2) = 2 =&gt; 2 % [Tex] [/Tex](2) = 0 [Tex] [/Tex](3) = 2 =&gt; 3 % [Tex] [/Tex](3) = 1 Hence 2 satisfies the condition.Input: L = 12, R
6 min read
Count of elements having Euler's Totient value one less than itself
Given an array arr[] of N integers, and a range L to R, the task is to find the total number of elements in the array from index L to R that satisfies the following condition: [Tex]F(arr[i]) = arr[i] - 1 [/Tex] where F(x) is Euler's Totient Function. Examples: Input: arr[] = {2, 4, 5, 8}, L = 1, R = 3 Output: 2 Explanation: Here in the given range,
9 min read
Sum of Euler Totient Functions obtained for each divisor of N
Given a positive integer N, the task is to find the sum of the Euler Totient Function for all the divisor of the given number N. Examples: Input: N = 3Output: 3Explanation:Divisors of 3 are {1, 3}. The Euler totient function for the values 1 and 3 are 1 and 2 respectively.Therefore, the required sum is 1 + 2 = 3. Input: N = 6Output: 6 Naive Approac
3 min read
Euler Totient for Competitive Programming
What is Euler Totient function(ETF)?Euler Totient Function or Phi-function for 'n', gives the count of integers in range '1' to 'n' that are co-prime to 'n'. It is denoted by [Tex]\phi(n) [/Tex].For example the below table shows the ETF value of first 15 positive integers: 3 Important Properties of Euler Totient Funtion:If p is prime number:[Tex]\p
8 min read
Highly Totient Number
A highly totient number k is an integer that has more solutions to the equation ?(x) = k, where ? is Euler's totient functionThe sequence : 1, 2, 4, 8, 12, 24, 48, 72, 144, 240, 432, 480, 576, 720, 1152, 1440, 2880, 4320, 5760, 8640 Explanation : 1 has 2 solutions2 has 3 solutions4 has 4 solutions8 has 5 solutions12 has 6 solutions24 has 10 solutio
9 min read
Perfect totient number
A Perfect totient number is an integer that is equal to the sum of its iterated totients. Perfect totient number is denoted by [Tex]\varphi^i(n) [/Tex]. For example: [Tex]\varphi^i (9) = 6 [/Tex], Now, [Tex]\varphi^i (6) = 2 [/Tex], [Tex]\varphi^i (2) = 1 [/Tex]and 9 == 6 + 2 + 1, Therefore, 9 is a perfect totient number. Check if N is a Perfect to
7 min read
Euler Method for solving differential equation
Given a differential equation dy/dx = f(x, y) with initial condition y(x0) = y0. Find its approximate solution using Euler method. Euler Method : In mathematics and computational science, the Euler method (also called forward Euler method) is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial v
6 min read
Euler's Four Square Identity
According to Euler's four square identity, the product of any two numbers a and b can be expressed as a sum of four squares if a and b both can individually be expressed as the sum of four squares.Mathematically, if a = [Tex]c1^2 + c2^2 + c3^2 + c4^2 [/Tex]and b = [Tex]d1^2 + d2^2 + d3^2 + d4^2 [/Tex]Then, a * b = [Tex]e1^2 + e2^2 + e3^2 + e4^2 [/T
15+ min read
Euler Tour of Tree
A Tree is a generalization of connected graph where it has N nodes that will have exactly N-1 edges, i.e one edge between every pair of vertices. Find the Euler tour of tree represented by adjacency list. Examples: Input : Output : 1 2 3 2 4 2 1 Input : Output : 1 5 4 2 4 3 4 5 1 Euler tour is defined as a way of traversing tree such that each vert
6 min read
Euclid Euler Theorem
According to Euclid Euler Theorem, a perfect number which is even, can be represented in the form [Tex](2^n - 1)*(2^n / 2) )) [/Tex]where n is a prime number and [Tex]2^n - 1 [/Tex]is a Mersenne prime number. It is a product of a power of 2 with a Mersenne prime number. This theorem establishes a connection between a Mersenne prime and an even perf
10 min read
Euler tour of Binary Tree
Given a binary tree where each node can have at most two child nodes, the task is to find the Euler tour of the binary tree. Euler tour is represented by a pointer to the topmost node in the tree. If the tree is empty, then value of root is NULL. Examples: Input : Output: 1 5 4 2 4 3 4 5 1 Approach: First, start with root node 1, Euler[0]=1 Go to l
7 min read
Total nodes traversed in Euler Tour Tree
Euler tour of tree has been already discussed which flattens the hierarchical structure of tree into array which contains exactly 2*N-1 values. In this post, the task is to prove that the degree of Euler Tour Tree is 2 times the number of nodes minus one. Here degree means the total number of nodes get traversed in Euler Tour. Examples: Input: Outp
7 min read
Predictor-Corrector or Modified-Euler method for solving Differential equation
For a given differential equation [Tex]\frac{dy}{dx}=f(x, y) [/Tex]with initial condition [Tex]y(x_0)=y_0 [/Tex]find the approximate solution using Predictor-Corrector method.Predictor-Corrector Method : The predictor-corrector method is also known as Modified-Euler method. In the Euler method, the tangent is drawn at a point and slope is calculate
13 min read
Euler zigzag numbers ( Alternating Permutation )
Euler Zigzag numbers is a sequence of integers which is a number of arrangements of those numbers so that each entry is alternately greater or less than the preceding entry.c1, c2, c3, c4 is Alternating permutation where c1 &lt; c2 c3 &lt; c2 c3 &lt; c4...zigzag numbers are as follows 1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936, 50521 ......For a given
7 min read
Check if a number is Euler Pseudoprime
Given an integer N and a base number A, the task is to check whether N is a Euler Pseudoprime to the given base A. An integer N is called Euler Pseudoprime to the base A, if A &gt; 0 and N is an odd composite number.A and N are co-prime i.e. GCD(A, N) = 1.A(N - 1) / 2 % N is either 1 or N - 1. Examples: Input: N = 121, A = 3 Output: YesInput: N = 3
9 min read
Euler's Factorization method
Given a number N, the task is to find the factors of N. Examples: Input: N = 1000009 Output: 293 3413 Explanation: 293 * 3413 = 1000009Input: N = 100000 Output: 800 125 Explanation: 800 * 125 = 100000 Euler's Factorization method: Euler's factorization method works on the principle that all the numbers N which can be written as the sum of two power
15+ min read
Conversion of an Undirected Graph to a Directed Euler Circuit
Given an undirected graph with V nodes (say numbered from 1 to V) and E edges, the task is to check whether the graph is an Euler Graph or not and if so then convert it into a Directed Euler Circuit. A Directed Euler Circuit is a directed graph such that if you start traversing the graph from any node and travel through each edge exactly once you w
10 min read
Euler Circuit in a Directed Graph
Eulerian Path is a path in graph that visits every edge exactly once. Eulerian Circuit is an Eulerian Path which starts and ends on the same vertex. A graph is said to be eulerian if it has a eulerian cycle. We have discussed eulerian circuit for an undirected graph. In this post, the same is discussed for a directed graph. For example, the followi
13 min read
Euler's criterion (Check if square root under modulo p exists)
Given a number 'n' and a prime p, find if square root of n under modulo p exists or not. A number x is square root of n under modulo p if (x*x)%p = n%p. Examples : Input: n = 2, p = 5 Output: false There doesn't exist a number x such that (x*x)%5 is 2 Input: n = 2, p = 7 Output: true There exists a number x such that (x*x)%7 is 2. The number is 3.
11 min read
Project Euler
What is Project Euler? Project Euler is a series of challenging problems that require mathematical and programming skills. Somebody who enjoys learning new area of mathematics, project Euler is going to be a fun journey. Where are the problems ? The problems are right here in their official archive. Let's solve a problem from the archive and unders
3 min read
Minimum edges required to add to make Euler Circuit
Given a undirected graph of n nodes and m edges. The task is to find minimum edges required to make Euler Circuit in the given graph. Examples: Input : n = 3, m = 2 Edges[] = {{1, 2}, {2, 3}} Output : 1By connecting 1 to 3, we can create a Euler Circuit. For a Euler Circuit to exist in the graph we require that every node should have even degree be
11 min read
Spt function or Smallest Parts Function of a given number
Given an integer N, the task is to find the Spt Function of the number N. The spt function (smallest parts function) is a function in number theory that counts the sum of the number of smallest parts in each partition of a positive integer. It is related to the partition function. For example Partitions of 4 are [1, 1, 1, 1], [1, 1, 2], [2, 2], [1,
9 min read
Unbounded Binary Search Example (Find the point where a monotonically increasing function becomes positive first time)
Given a function 'int f(unsigned int x)' which takes a non-negative integer 'x' as input and returns an integer as output. The function is monotonically increasing with respect to the value of x, i.e., the value of f(x+1) is greater than f(x) for every input x. Find the value 'n' where f() becomes positive for the first time. Since f() is monotonic
11 min read
Sorting using trivial hash function
We have read about various sorting algorithms such as heap sort, bubble sort, merge sort and others. Here we will see how can we sort N elements using a hash array. But this algorithm has a limitation. We can sort only those N elements, where the value of elements is not large (typically not above 10^6). Examples: Input : 9 4 3 5 8 Output : 3 4 5 8
15+ min read
three90RightbarBannerImg