Number of functions from $\mathbb{Z}_m$ to $\mathbb{Z}_n$. I'm just wondering if there's a generalised way of computing the number of functions from $\mathbb{Z}_m$ to $\mathbb{Z}_n$, and additionally the number of one-to-one and onto functions between $\mathbb{Z}_m$ and $\mathbb{Z}_n$.
 A: We will count functions $f:[m]\to [n],$ of the general, injective, and surjective kind. You did not mention anything about preserving group or ring structures, so we will not pay attention to that.

*

*The number of general functions is $$\underbrace{n\cdot n\cdots n}_{m \text{ copies of } n}=n^m.$$

*Assuming $n\ge m,$ he number of injective functions is $$n(n-1)\cdots (n-m+1)=\frac{n!}{(n-m)!}.$$ If $n<m,$ then there are no injective functions.

*We can find the number of surjective functions using the symmetric principle of inclusion-exclusion. The idea is to let $F_i$ be the number of functions $g:[m]\to[n]$ where $i$ is missed in the range (others might be missed too). Using the first formula, we get $$n^m - \left|\bigcup_{i=1}^{n}F_i\right|=\sum_{i=0}^{n}{(-1)^i\binom{n}{i}(n-i)^m}.$$ You should be able to figure out the details of that computation if you study the principle of inclusion-exclusion. This formula gives $0$ if $n>m,$ as it should since there are no surjections in that case.

This is a part of a larger set of problems called the Twelvefold Way, which are covered fairly formally in Richard Stanley's Enumerative Combinatorics.
