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 functions are there between two finite sets?

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$.

  • $\begingroup$ For readers in 2020: 1. you will have to understand exactly-none formula of Inclusion-Exclusion Principle, 2. Let $E_0$ means exactly $0$ of the elements in $\Bbb B$ that you sure it(they) won't be used as function value(s), then $E_0$ indeed counts the number of onto functions: $$\begin{align}\textrm{onto}(m,n)=E_0&=\sum\limits_{j=0}^{n}(-1)^j\binom{j}{0}S_j\\&=\sum\limits_{j=0}^{n}(-1)^j\color{blue}{\left[\binom{n}{j}(n-j)^m\right]},\end{align}$$ where the blue part $S_j$ is defined as: you're sure that $j$ of the values won't be the function values. $\endgroup$ Sep 3, 2020 at 18:51

5 Answers 5


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(n-2)^m$ to get


which can be expressed more systematically as


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 $n!$ then gives the number of ways of assigning those pieces to the $n$ elements of $\Bbb B$.

  • $\begingroup$ its awesome comment! What is your reference? @BrianM.Scott I am looking for a book or article about it. $\endgroup$
    – user1062
    Oct 2, 2021 at 15:15
  • $\begingroup$ @user1062: Any decent book on combinatorics, and especially enumerative combinatorics, will have everything that I used. This brief introductory text [PDF] has everything that I used. Some of the other items in the list here would also do; I like the two books by Miklós Bóna. All of these will also have far more than you need, however. $\endgroup$ Oct 2, 2021 at 21:24
  • $\begingroup$ thank you very much:) @BrianM.Scott $\endgroup$
    – user1062
    Oct 2, 2021 at 21:33
  • $\begingroup$ @user1062: You’re very welcome. $\endgroup$ Oct 4, 2021 at 1:27

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.


Another way to think about this that more naturally shows how this problem maps to Stirling numbers of the second kind comes from Bogart in Introductory Combinatorics. There is an exercise that asks us to come up with a recurrence relation for the number of functions from a set onto another set that has a similar form to the recursive formula for binomial coefficients. To that end, we define a function $f(m,n)$ that is the number of functions from a set of size $m$ onto a set of size $n$.

Without loss of generality, we take set $\mathbb A$ to be integers $n \in \mathbb N, 1\leq n \leq m$, i.e., $\{1,...,m\}$. Now we consider the number of elements in $\mathbb A $ that map to the same element of $\mathbb B$ as $m$ does. The functions in which at least one other element of $\mathbb A $ maps to the same element as $m$ are counted by $n\times f(m-1,n)$ and the functions in which no other elements of $\mathbb A $ map to the same element as $m$ are counted by $n\times f(m-1,n-1)$, so we get

$f(m,n) = nf(m-1,n) + nf(m-1,n-1)$

Now, I'm no good at solving recurrence relations, but thanks to Brian we already have a potential formula for $f(m,n)$ so we can substitute it in and see if it checks out:

$f(m,n) = nn!{m-1\brace n} +n(n-1)!{m-1\brace n-1} = n!(n{m-1\brace n} +{m-1\brace n-1})=n!{m\brace n}$


one can also get this by the following correspondence:No of onto functions is same as no of ways of distributing m distinct objects into n distinct containers(each container can receive any no. of objects)such that none of the container is left empty


By the inclusion-exclusion principle, a Set $B$ containing $N$ distinct objects, with set of properties $B_1, B_2, \ldots, B_n$ (that the objects of the set $B$ may possess), and $N(B_i)$ being the number of objects having property $B_i$,the number of objects in $B$ having none of the properties $B_1, B_2, \ldots, B_n$ is given by


$=N-\left(\sum\limits_{i=1}^{n} N(B_i)-\underset{1\leq i\neq j \leq n}{\sum\sum}N(B_i\cap B_j)+\underset{1\leq i\neq j \neq k \leq n}{\sum\sum\sum}N(B_i\cap B_j \cap B_k) - \ldots + (-1)^n N\left(\bigcap\limits_{i=1}^n B_i\right)\right)$

Now, for the mapping $f: A \to B$, with $A=\{a_1,a_2,\ldots,a_m\}$ and $B=\{b_1,b_2,\ldots,b_n\}$, s.t., $|A|=m$ and $|B|=n$, with $m\leq n$, let's define the property $B_i$ as $b_i \not \in B$, $\forall{i\in\{1, 2, \ldots, n\}}$, with $N=n^m$ being total number of possible mappings.

Then we have the number of onto functions


$=n^m-\left(n(n-1)^m-{n \choose 2}(n-2)^m+\ldots+ (-1)^{n-1}{n \choose n-1}1^m\right)$

$=\sum\limits_{k=0}^{n}(-1)^k{n \choose k}(n-k)^m$

(Notice the similarity of the above expression with that of The Matching Problem/Derangements - n letters to n people)


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