How many idempotent elements does the ring ${\bf Z}_n$ contain? Let $R$ be a ring. An element $x$ in $R$ is said to be idempotent if $x^2=x$. For a specific $n\in{\bf Z}_+$ which is not very large, say, $n=20$, one can calculate one by one to find that there are four idempotent elements: $x=0,1,5,16$. So here is my question:

Is there a general result which tells the number of the idempotent elements of ${\bf Z}_n$? 

 A: In $\Bbb{Z}_n$ the relation $x^2=x$ is equivalent to $(x-1)x\equiv 0 ( mod \ n)$, that is $n | x(x-1)$. This is an easy way to calculate all idempotent elements for small $n$. In general, you need to consider the factorization of $n$ in prime factors and note that $x,x-1$ are coprime, and if one prime number divides one of them, it can't divide the other.
A: Hint $ $ Idempotents in $\,\mathbb Z/n\,$ correspond to coprime factorizations of $\,n\,$ i.e. $\, n = a\:\!b,\ (a,b) = 1\,.\ $ Indeed, notice that $\,p^k\mid e(e-1)\iff p^k\ |\ e\,$ or $\,p^k\ |\ e-1.\,$ It is easy to see that there are $\, 2^k\,$ such factorizations, where $\,k\,$ is the number of distinct prime factors of $\, n.$
See here for an example we we compute all these idempotents using CRT.
A: $n\geq 1$ be an integer and consider the ring $R=\mathbb{Z}/\mathbb{Z}_{n}$. First of all, we can see that the number of all the idempotents elements of $R$ is $2^m$, where $m$ is the number of distinct prime divisor of $n$.
$\textbf{Proof:} $Let $n=\prod\limits_{i=1}^{n} p_{i}n^{i}$ be the prime factorization of $n$ and $R_{i}=\mathbb{Z}/p_{i}^{n_{i}}$. By the Chinese Remainder theorem we have $R\cong R_{1}$ x $\cdots$ x $R_{m}$, say $(1)$.
$\textbf{Claim:} $ If $p$ is a prime and $l>0$ is an integer, then the only idempotent element of $\mathbb{Z}/p^{l}\mathbb{Z}$ are $0$ and $1$.
$\textbf{Proof:} $So we wnat to show that modulo $p^{l}$, the equation $x^{2}\equiv x$ (mod $p^{l})$ has only two trivial solutions $x=0,1$. Suppose that $x\neq 0$ is a solution of $x^{2}\equiv x$ (mod $p^{l})$. We will show that $x\equiv 1$ (mod $p^{l})$.
Let $x=p^{r}s$, where $0\leq r<1$, and gcd($s, p)=1$. Then $s(p^{r}s-1)\equiv 0$ (mod $p^{l-r})$ which give us $p^{r}s\equiv 1$ (mod $p^{l-r})$. Thus $r=0$ and hence $x\equiv s\equiv 1$ (mod $p^{l})$. It is clear now from (1) and claim that the number of idempotents of ring  $R=\mathbb{Z}/\mathbb{Z}_{n}$ is $2^{m}$.
Now we can solve any question for $\mathbb{Z}/\mathbb{Z}_{n}$.
By the above $R=\mathbb{Z}/\mathbb{Z}_{20}$, we know that $R$ has $4$ idempotents, it is clear two of them being $0, 1$ (mod $20$). Let $R_{1}=\mathbb{Z}/\mathbb{Z}_{4}$, $R_{2}=\mathbb{Z}/\mathbb{Z}_{5}$. Then $R\cong R_{1}$ x $R_{2}$. All idempotents of  $R_{1}$ x $R_{2}$ are $(0, 0)$, $(1, 0)$, $(0, 1)$,  $(1, 1)$. 
So we just need to find preimage of each idempotent in $R$. Obviously, the preimages of  $(0, 0)$ and $(1, 1)$ are $0$ and $1$ (mod $20$) respectively. Now let let's find the preimage of, say $b=(0, 1)$. Let $a=m+20\mathbb{Z}$ be the preimage of $b$. Then the image of $a$ is $(m+4\mathbb{Z}, m+5\mathbb{Z})=b=(4\mathbb{Z}, 1+5\mathbb{Z})$. So $m$ divisible by $4$ and it is equiavlent to $1$ (mod $5$). It follows that $a=16+20\mathbb{Z}$. Similarly, we can find another idempotent 5 which is preimage of $(1,0)$.
A: If $n=p_1^{m_1}\cdots p_k^{m_k}$ is the factorization of $n$ as a product of powers of distinct primes, then the ring $\mathbb Z/n\mathbb Z$ is isomorphic to the product $\mathbb Z/p_1^{m_1}\mathbb Z\times\cdots\times \mathbb Z/p_k^{m_k}\mathbb Z$. It is easy to reduce the problem of counting idempotent elements in this direct product to counting them in each factor.
Can you do that?
A: 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(\prod\limits_{\substack{i=1,\\
i\neq k}}^{n}p^{c_{i}}_{i})\mathbb{Z}$ such that $h_{k}-1\in p^{c_{k}}_{k}\mathbb{Z}$.
