Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

What are the number of onto functions from a set $\Bbb A $ containing m elements to a set $\Bbb B$ containing n elements.

I found that if m = 4 and n = 2 the number of onto functions is 14.

But is there a way to generalise this using a formula? If yes, what is this formula and how is it derived?


I referred to this question but my doubt was not cleared: how many one to one and onto function?

If not, Then what is the standard way of doing it?

If you explain this to me with an example please explain with the example of m = 5 and n = 3.

important This is a part of my curriculum and I have an important exam tomorrow. So, I request you to make haste in giving me the solution. I will be very grateful to you.

share|improve this question

2 Answers 2

up vote 14 down vote accepted

Obviously if $m<n$, there are no function from $\Bbb A$ onto $\Bbb B$, so assume that $m\ge n$. We’ll use an inclusion-exclusion argument. There are $n^m$ functions of all kinds from $\Bbb A$ to $\Bbb B$. If $b\in\Bbb B$, there are $(n-1)^m$ functions from $\Bbb A$ to $\Bbb B\setminus\{b\}$, i.e., functions whose ranges do not include $b$. We need to subtract these from the original $n^m$, and we need to do it for each of the $n$ members of $\Bbb B$, so a better approximation is $n^m-n(n-1)^m$.

Unfortunately, a function whose range misses two members of $\Bbb B$ gets subtracted twice in that computation, and it should be subtracted only once. Thus, we have to add back in the functions whose ranges miss at least two points of $\Bbb B$. If $b_0,b_1\in\Bbb B$, there are $(n-2)^m$ functions from $\Bbb A$ to $\Bbb B\setminus\{b_0,b_1\}$, and there are $\binom{n}2$ pairs of points of $\Bbb B$, so we have to add back in $\binom{n}2(m-2)^n$ to get


which can be expressed more systematically as


(Note: corrected the error )

Unfortunately, this over-corrects in the other direction, by adding back in too much. The final result, when the entire inclusion-exclusion computation is made, is


which can also be written $$n!{m\brace n}\;,$$ where ${m\brace n}$ is a Stirling number of the second kind. The Stirling number gives the number of ways of dividing up $\Bbb A$ into $n$ non-empty pieces, and the $m!$ then gives the number of ways of assigning those pieces to the $n$ elements of $\Bbb B$.

share|improve this answer

The answer is a little complicated. It is $n!S(m,n)$, where $S(m,n)$ is the appropriate Stirling Number of the second kind.

There are nice recursions for the $S(m,n)$, but no simple closed-form formula.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.