![]() |
VOOZH | about |
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,
We have discussed different methods to compute Euler Totient function that work well for single input. In problems where we have to call Euler’s Totient Function many times like 10^5 times, simple solution will result in TLE(Time limit Exceeded). The idea is to use Sieve of Eratosthenes.
Find all prime numbers upto maximum limit say 10^5 using Sieve of Eratosthenes.
To compute ?(n), we do following.
Output:
10 12 30 40 32 60 72 100
Time Complexity: O(MAX*log(MAX)+sqrt(n/log(n)))
Auxiliary Space: O(MAX)