Purpose
Euler’s totient function. For , it gives , the number of natural numbers up to that are coprime to .
When the prime factorization of n is expressed as
,
we calculate using this transformation.
Time Complexity
Usage
int res = euler_phi(n);Implementation
int euler_phi(int n) { int ret = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { ret -= ret / i; do n /= i; while (n % i == 0); } } return n > 1 ? ret - ret / n : ret;}