TITLE: The number of ring homomorphisms from $\mathbb{Z}_m$ to $\mathbb{Z}_n$
QUESTION [10 upvotes]: I face the problem of finding how many non-trivial ring or group homomorphisms there are from $\mathbb{Z}_m$ to $\mathbb{Z}_n$, where $m<n$. Is there any general formula? 
At the moment, I want to know how many ring homomorphisms there are when $m=12,n=28$.
Please help.

REPLY [18 votes]: A ring homomorphism $f:\mathbb Z_m\to \mathbb Z_n$ is uniquely determined by the conditions: $mf(1)=0$ and $f(1)^2=f(1)$.
In order to find out how many ring homomorphisms there are we have to count the number of elements of the set $\{e\in\mathbb Z_n:e^2=e,me=0\}$. It is not hard to see that this equals $2^{\omega(n)-\omega(n/(m,n))}$, where $\omega(a)$ is the number of distinct prime factors of $a$. (See Gallian and Van Buskirk, The Number of Homomorphisms from $\mathbb Z_m$ to $\mathbb Z_n$, AMM, 91(1984), 196-197.)
For particular cases we don't need the above result. For instance, if $m=12$ and $n=28$ we get $0=me=12e$  in $\Bbb Z_{28}$ iff $28\mid 12e$ iff $\,7\mid e,\,$ so $  f(1)\in\{0,7,14,21\}$ and only $0$ and $21$ are idempotent in $\Bbb Z_{28},$ so there are two ring homomorphisms from $\mathbb Z_{12}$ to $\mathbb Z_{28}$.
Edit. If one asks $f(1)=1$ then the problem becomes trivial: there is a unitary ring homomorphism iff $n\mid m$.