TITLE: Idempotents in $\mathbb Z_n$
QUESTION [30 upvotes]: An element $a$ of the ring $(P,+,\cdot)$ is called idempotent if $a^2=a$. An idempotent $a$ is called nontrivial if $a \neq 0$ and $a \neq 1$. 
My question concerns idempotents in rings $\mathbb Z_n$, with addition and multiplication modulo $n$, where $n$ is natural number. Obviously when $n$ is a prime number then there is no nontrivial idempotent. If $n$ is nonprime it may happen, for example $n=4, n=9$, that also there is no.

Is it known, in general, for what $n$ there are nontrivial idempotents and what is a form of such idempotents?

REPLY [2 votes]: Let $m=p^{c_{1}}_{1}...p^{c_{n}}_{n}$ be a prime factorization of an integer $m$ with $c_{i}\geq1$ and $p_{i}$ are distinct prime numbers. Then the ring $\mathbb{Z}/m\mathbb{Z}$ has $2^{n}$ idempotents and (modulo $m$) these are precisely of the form $\sum\limits_{k=1}^{n}h_{k}\epsilon_{k}$ where $\epsilon_{k}\in\{0,1\}$ and $h_{k}\in\left(\prod\limits_{\substack{i=1,\\
i\neq k}}^{n}p^{c_{i}}_{i}\right)\mathbb{Z}$ such that $h_{k}-1\in p^{c_{k}}_{k}\mathbb{Z}$.