Find all values such that $\phi(n)=n-2$

elementary-number-theorymodular arithmetic

I'm currently working in the following Euler's theorem exercise:

Find all values such that $\phi(n)=n-2$

A simple calculation gave me the first one based on the formula $\phi(n^k) = n^k-n^{k-1}$, so with $n=4$, checking similar questions here I'm trying to find other numbers based on:

$$\phi(n)=n\prod_{p|n}(1-1/p)$$

But I've been unable to use it to solve my problem, any help will be really appreciated.

Best Answer

If $\phi(n)=n-2$, there are exactly two integers between $2$ and $n$ that have a common factor with $n$. For $n>1$ (which we obviously need), $n$ is one of them. That leaves one other.

If $n$ is prime, then there are no others and $\phi(n)=n-1$. Nope.

If $n$ is not prime, there's some prime $p<n$ such that $p|n$. Then $p$ and $\frac np$ both have a common factor with $n$, and are both different from $n$. If $p\neq \frac np$, that's three integers with a common factor right there. Nope.

That leaves the case $n=p^2$ for $p$ a prime. In this case, we can easily see that $\phi(p^2)=p^2-p$, leaving $p=2$ and $n=4$ the only option. There is only one solution to $\phi(n)=n-2$, namely $n=4$.