# 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.

```Φ(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: Please solve it on “PRACTICE ” first, before moving on to the solution.

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.

1. Initialize result as n.
2. Iterate through all primes smaller than or equal to square root of n (This is where it is different from simple methods. Instead of iterating through all numbers less than or equal to square root, we iterate through only primes). Let the current prime number be p. We check if p divides n, if yes, we remove all occurrences of p from n by repeatedly dividing it with n. We also reduce our result by n/p (these many numbers will not have GCD as 1 with n).
3. Finally we return result.
```// C++ program to efficiently compute values
// of euler totient function for multiple inputs.
#include <bits/stdc++.h>
using namespace std;

#define ll long long
const int MAX = 100001;

// Stores prime numbers upto MAX - 1 values
vector<ll> p;

// Finds prime numbers upto MAX-1 and
// stores them in vector p
void sieve()
{
ll isPrime[MAX+1];

for (ll i = 2; i<= MAX; i++)
{
// if prime[i] is not marked before
if (isPrime[i] == 0)
{
// fill vector for every newly
// encountered prime
p.push_back(i);

// run this loop till square root of MAX,
// mark the index i * j as not prime
for (ll j = 2; i * j<= MAX; j++)
isPrime[i * j]= 1;
}
}
}

// function to find totient of n
ll phi(ll n)
{
ll res = n;

// this loop runs sqrt(n / ln(n)) times
for (ll i=0; p[i]*p[i] <= n; i++)
{
if (n % p[i]== 0)
{
// subtract multiples of p[i] from r
res -= (res / p[i]);

// Remove all occurrences of p[i] in n
while (n % p[i]== 0)
n /= p[i];
}
}

// when n has prime factor greater
// than sqrt(n)
if (n > 1)
res -= (res / n);

return res;
}

// Driver code
int main()
{
// preprocess all prime numbers upto 10 ^ 5
sieve();
cout << phi(11) << "\n";
cout << phi(21) << "\n";
cout << phi(31) << "\n";
cout << phi(41) << "\n";
cout << phi(51) << "\n";
cout << phi(61) << "\n";
cout << phi(91) << "\n";
cout << phi(101) << "\n";
return 0;
}
```

Output:

```10
12
30
40
32
60
72
100
```

This article is contributed by Abhishek Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.3 Average Difficulty : 2.3/5.0
Based on 3 vote(s)

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.