The twelvefold way explained
Handing out gifts, sharing sweets, seating guests, forming teams, writing a number as a sum: a surprising number of counting problems are secretly the same one. Put n balls into k boxes and ask how many ways there are. The answer depends on only three things, and the twelve combinations of them, known as the twelvefold way, form a map of the most important numbers in combinatorics.
Three questions
Before counting, settle three things. Are the balls distinct, like people or numbered tickets, or identical, like sweets of one kind? Are the boxes distinct, like children or days of the week, or identical, like unnamed teams or piles? And may a box hold any number of balls, at most one, or at least one? Two choices for the balls, two for the boxes and three for the rule make twelve problems. Getting these three answers right is most of the work: the formula follows.
Distinct balls, distinct boxes
Every ball chooses a box, independently of the others, so the multiplication principle gives kⁿ ways: five different gifts can go to three children in 3⁵ = 243 ways. These are all the functions from a set of n to a set of k. If no box may hold two balls, the first ball has k boxes, the next k − 1, and so on, which gives k! / (k − n)!, the number of ways to seat four guests on six chairs, 360. If every box must get a ball, count the onto functions: split the balls into k groups and give the groups to the boxes, k! · S(n, k), 150 for the gifts if every child gets at least one.
Identical balls, distinct boxes
Now only the number of balls in each box matters. Draw the balls as stars and separate the boxes with k − 1 bars: ★★|★||★★★ puts two, one, none and three balls in four boxes. Every row of n stars and k − 1 bars is one way, so the count is C(n + k − 1, n): ten identical sweets can be shared among four children in 286 ways. If every child must get at least one, give each one sweet first and share the rest, or cut the row of balls in k − 1 of its n − 1 gaps: C(n − 1, k − 1) = 84. With at most one ball per box you only choose which boxes are filled: C(k, n).
Distinct balls, identical boxes: Stirling and Bell numbers
When the boxes look alike, it no longer matters which box a group lands in, only which balls end up together. The number of ways to split n distinct balls into exactly k non-empty groups is the Stirling number of the second kind S(n, k). They follow a simple rule: the last ball either sits alone, S(n − 1, k − 1), or joins one of the k groups of the others, k · S(n − 1, k). So S(6, 3) = 90 ways to make three teams from six people. Adding S(n, k) over every k gives the Bell number B(n), all the ways to group n things: 1, 1, 2, 5, 15, 52, 203 and on.
Identical balls, identical boxes: partitions
If neither balls nor boxes can be told apart, only the sizes of the piles remain, and a way to fill the boxes is a way to write n as a sum, largest part first. These are the partitions of n. There are p(n, k) partitions into exactly k parts, with p(n, k) = p(n − 1, k − 1) + p(n − k, k): either a part is 1, or every part is at least 2 and you can take 1 from each. The number 8 is a sum of three parts in five ways, and seven identical books fit into three identical boxes in eight ways. With enough boxes you get all p(n) partitions of n, which grow fast: p(100) is 190,569,292.
The easy cells, and the empty box
Two cells are almost trivial: if the boxes are identical and no box may hold two balls, every ball is alone in a box, and since the boxes look the same there is exactly one way, provided there are enough boxes, and none if there are not. The edges deserve care too. With no balls there is exactly one way, leaving every box empty, so kⁿ with n = 0 is 1, and S(0, 0) and p(0, 0) are both 1. With balls but no boxes there is no way at all. With more balls than boxes, at most one per box is impossible; with fewer, at least one per box is.
Where the name comes from
The table was set out by the mathematician Gian-Carlo Rota in lectures at MIT, and Richard Stanley made it widely known in the first volume of Enumerative Combinatorics in 1986; the name the twelvefold way was suggested by Joel Spencer, after the eightfold way of Buddhism and of particle physics. Its parts are much older: Stirling numbers go back to James Stirling in 1730, partitions to Leonhard Euler, who found the generating function of p(n) in the 1740s, and Srinivasa Ramanujan and G. H. Hardy found a formula for p(n) in 1918.
Explore it
Open the balls into boxes calculator to enter n and k and see all twelve counts side by side, exact to the last digit. Click a cell to see its formula worked out for your numbers and why it counts what it counts, try the worked examples from gifts to teams, and look through every way to share out the balls, drawn as boxes with balls.
Frequently asked questions
Why is the count larger when the balls are distinct?
Because more outcomes can be told apart. With distinct balls, giving ball 1 to box A and ball 2 to box B differs from the other way round; with identical balls both are just one ball in each box. Making the boxes identical merges outcomes in the same way, so the identical-identical row always has the smallest numbers.
What does onto mean here?
A function is onto, or surjective, when every element of the target is hit. With balls and boxes, it means no box stays empty: the at least one column. Likewise a one-to-one or injective function sends no two balls to the same box: the at most one column.
How are Bell numbers related to Stirling numbers?
The Bell number B(n) is the sum of the Stirling numbers S(n, k) over every k from 0 to n: it counts all the ways to group n distinct things, whatever the number of groups. B(4) = 1 + 7 + 6 + 1 = 15.
Is there a formula for the number of partitions?
No simple closed formula, but there are fast ways to compute p(n): the table of the calculator adds the partitions part size by part size, and Euler's pentagonal number theorem gives a recurrence. Hardy and Ramanujan found that p(n) is roughly e^(π√(2n/3)) / (4n√3), and Rademacher later turned that into an exact series.
Ready to try it?
Open PictorX in your browser — no install, no credit card required to start.
Open the balls into boxes calculator