Professional Documents
Culture Documents
Euler's Totient Function - GeeksforGeeks
Euler's Totient Function - GeeksforGeeks
Euler's Totient Function - GeeksforGeeks
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
WHAT’S NEW
gcd(1, 5) is 1, gcd(2, 5) is 1,
gcd(3, 5) is 1 and gcd(4, 5) is 1
Data Structures and
Φ(6) = 2 Algorithms – Self Paced
gcd(1, 6) is 1 and gcd(5, 6) is 1, Course
View Details
Prime Numbers
Output :
The above code calls gcd function O(n) times. The time complexit y
formula which states that the value of totient functions is below the
The formula basically says that the value of Φ(n) is equal to n Write a program to reverse digits of a
multiplied by product of (1 – 1/p) for all prime factors p of n. For
number
example value of Φ(6) = 6 * (1-1/2) * (1 – 1/3) = 2.
Program for factorial of a number
We can find all prime factors using the idea used in this post.
int phi(int n)
{
// Initialize result as n
float result = n;
return (int)result;
}
// Driver code
int main()
{
int n;
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
idea is to count all prime factors and their multiples and subtract
this count from n to get the totient function value (Prime factors
1) Initialize result as n
2) Consider every number 'p' (where 'p' varies from 2 t
If p divides n, then do following
a) Subtract all multiples of p from 1 to n [all mult
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 mult
of n from result.
result -= result / p;
}
}
return result;
}
// Driver code
int main()
{
int n;
for(n = 1; n <= 10; n++)
{
cout << "Phi" << "("
<< n << ")" << " = "
<< phi(n) << endl;
}
return 0;
}
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
n = 10.
Initialize: result = 10
n.
theorem :
aΦ(n) Φ 1 (mod n)
In the par ticular case when m is prime say p, Euler ’s theorem turns
ap-1 Φ 1 (mod p)
addition is Φ(n).
Related Ar ticle :
References :
http://e-maxx.ru/algo/euler_function
http://en.wikipedia.org/wiki/Euler%27s_totient_function
Attention reader! Don’t stop learning now. Get hold of all the
Like 0
Previous Next
Euler's Totient function Write an iterative
for all numbers smaller O(Log y) function for
than or equal to n pow(x, y)
Probability of Euler's
02
Totient Function in a
range [L, R] to be Count of elements
06
divisible by M having Euler's Totient
31, Jul 20 value one less than
itself
Check if Euler Totient 03, Jul 20
Euler's Totient
04
function for all
numbers smaller Highly Totient
08
than or equal to n Number
14, Nov 15 30, Jul 19
Ar ticle Contributed By :
GeeksforGeeks
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments