It is well known that if $n$ is an odd prime, then the equation $x^2=1$ has exactly two solutions in $\mathbb{Z}/n\mathbb{Z}$, namely $-1$ and $1$ (or, to be precise : the congruent class of $1$ modulo $n$ and its opposite).
Conversely, this can be true even if $n$ is not an odd prime. For example : $n=6$, or $n=9$ or $n=10$.
There are $24$ such values between $2$ and $100$.
My question :
What is a necessary and sufficient condition under which the equation $x^2=1$ has exactly two solutions in the ring $\mathbb{Z}/n\mathbb{Z}$ ?
My conjecture :
Numerical evidence, for $n\le 1000$, suggests that a N&S condition should be that $n$ has no more than two prime factors. Would it be true ? If so, how to prove it ?
Thanks.