Euler's Totient
HardAcc. 72.4%
+45 XP 25
The Co-Prime Count
The Euler's Totient Function $\phi(n)$ counts the integers $k$ such that $1 le k le n$ and $gcd(n, k) = 1$. Example: $\phi(9)$. Numbers co-prime to 9 are {1, 2, 4, 5, 7, 8}. So $\phi(9) = 6$.
The Assignment
Your function receives a parameter n.
- Initialize
resultton. - Use a loop to find prime factors of
n. - For every prime factor
p, updateresult = result * (1 - 1/p). - Print the final integer result.
01EXAMPLE 1
Input
n = 9Output
6Explanation: Numbers {1,2,4,5,7,8} are co-prime.
Constraints
- Use the prime product formula for efficiency.
- Handle n=1 as result 1.
MathLoopsPrimes
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
n9
Expected Output
6
Click RUN to test your solution