Euler's totient function, \$\varphi(n)\$, counts the number of integers \$1 \le k \le n\$ such that \$\gcd(k, n) = 1\$. For example, \$\varphi(9) = 6\$ as \$1,2,4,5,7,8\$ are all coprime to \$9\$. However, \$\varphi(n)\$ is not injective, meaning that there are distinct integers \$m, n\$ such that \$\varphi(m) = \varphi(n)\$. For example, \$\varphi(7) = \varphi(9) = 6\$.
The number of integers \$n\$ such that \$\varphi(n) = k\$, for each positive integer \$k\$, is given by A014197. To clarify this, consider the table
\$k\$ | Integers \$n\$ such that \$\varphi(n) = k\$ | How many? (aka A014197) |
---|---|---|
\$1\$ | \$1, 2\$ | \$2\$ |
\$2\$ | \$3, 4, 6\$ | \$3\$ |
\$3\$ | \$\$ | \$0\$ |
\$4\$ | \$5, 8, 10, 12\$ | \$4\$ |
\$5\$ | \$\$ | \$0\$ |
\$6\$ | \$7, 9, 14, 18\$ | \$4\$ |
\$7\$ | \$\$ | \$0\$ |
\$8\$ | \$15, 16, 20, 24, 30\$ | \$5\$ |
\$9\$ | \$\$ | \$0\$ |
\$10\$ | \$11, 22\$ | \$2\$ |
You are to implement A014197.
This is a standard sequence challenge. You may choose to do one of these three options:
- Take a positive integer \$k\$, and output the \$k\$th integer in the sequence (i.e. the number of integers \$n\$ such that \$\varphi(n) = k\$). Note that, due to this definition, you may not use 0 indexing.
- Take a positive integer \$k\$ and output the first \$k\$ integers in the sequence
- Output the entire sequence, in order, indefinitely
This is code-golf, so the shortest code in bytes wins.
The first 92 elements in the sequence are
2,3,0,4,0,4,0,5,0,2,0,6,0,0,0,6,0,4,0,5,0,2,0,10,0,0,0,2,0,2,0,7,0,0,0,8,0,0,0,9,0,4,0,3,0,2,0,11,0,0,0,2,0,2,0,3,0,2,0,9,0,0,0,8,0,2,0,0,0,2,0,17,0,0,0,0,0,2,0,10,0,2,0,6,0,0,0,6,0,0,0,3