Any number At most 1 At least 1 Distinct balls, distinct boxes Identical balls, distinct boxes Distinct balls, identical boxes Identical balls, identical boxes

    The balls are
    The boxes are
    Balls per box

    More information about the twelvefold way

    Balls into boxes calculator

    In how many ways can five gifts go to three children, ten sweets be shared among four, or six people be split into three teams? These are all the same question: how many ways are there to put n balls into k boxes? The answer depends on whether you can tell the balls apart, whether you can tell the boxes apart, and whether a box may stay empty or hold more than one ball. Together that gives twelve cases, the twelvefold way of combinatorics. Enter n and k to see all twelve answers at once, exact to the last digit, with the formula worked out for your numbers and every way drawn as boxes with balls. Enter the number of balls (n) and boxes (k), or choose a worked example. Say whether the balls are distinct or identical, whether the boxes are, and how many balls a box may hold. That gives the twelve ways of counting known as the twelvefold way, all shown at once in a table: click a cell to see its formula with your numbers and why it counts what it counts. Below, every way to share out the balls is drawn, for up to 16 balls and 10 boxes.

    How it works

    1. Enter the number of balls n and boxes k, or choose a worked example such as gifts for children, sweets to share or people in teams.
    2. Say whether the balls and the boxes are distinct or identical and how many balls a box may hold, or click one of the twelve cells of the table, to see that case's formula with your numbers and why it counts what it counts.
    3. Look through every way to share out the balls, drawn as boxes with balls, and copy the list or download it as CSV.

    Three questions, twelve answers

    Can you tell the balls apart? Can you tell the boxes apart? And may a box hold any number of balls, at most one, or at least one? Two times two times three gives twelve cases. With distinct balls and distinct boxes every ball simply picks a box: kⁿ ways, 3⁵ = 243 for five gifts and three children. If every box may hold at most one ball, the count is k! / (k − n)!, the ordered selections. With identical balls only the number in each box matters, and stars and bars give C(n + k − 1, n): 286 ways to share ten sweets among four children, and C(n − 1, k − 1) = 84 if every child gets at least one.

    When the boxes look alike

    If the boxes cannot be told apart, only the grouping counts. Splitting n distinct balls into k non-empty groups gives the Stirling numbers of the second kind S(n, k): six people can form three teams in S(6, 3) = 90 ways, and adding them up over every number of groups gives the Bell numbers, 52 for five people. If the balls are identical too, only the sizes of the piles remain, and you count the partitions of n: 8 can be written as a sum of three positive numbers in five ways, 6 + 1 + 1, 5 + 2 + 1, 4 + 3 + 1, 4 + 2 + 2 and 3 + 3 + 2. The table shows how all twelve counts hang together.

    Frequently asked questions

    What is the twelvefold way?

    A table of the twelve basic counting problems, first set out by Gian-Carlo Rota and made widely known by Richard Stanley. Each asks how many ways there are to put n balls into k boxes, and they differ in three things: whether the balls are distinct or identical, whether the boxes are, and whether a box may hold any number of balls, at most one, or at least one. Permutations, combinations, stars and bars, Stirling numbers and integer partitions all appear in it.

    How do I know whether my objects are distinct or identical?

    Ask whether swapping two of them gives a different result. People, numbered tickets and different gifts are distinct; sweets of one kind, coins of one value and votes are identical. For the boxes, ask whether it matters which box gets what: children, days and labelled jars are distinct boxes, while unnamed teams, groups or piles are identical ones.

    What are Stirling numbers of the second kind?

    S(n, k) is the number of ways to split a set of n distinct things into k non-empty groups, where the groups have no names or order. S(4, 2) = 7: the four ways to put one thing apart and the three ways to pair them up. They follow the rule S(n, k) = S(n − 1, k − 1) + k · S(n − 1, k): the last thing is either alone or joins one of the k groups of the others. Multiplied by k! they count the ways to fill k distinct boxes with none empty.

    What is the difference between a partition and a composition?

    Both write n as a sum of positive whole numbers. In a composition the order of the parts matters, so 2 + 1 and 1 + 2 are different: these are identical balls in distinct boxes, and n has C(n − 1, k − 1) compositions into k parts. In a partition the order does not matter: identical balls in identical boxes. The number 4 has eight compositions but only five partitions: 4, 3 + 1, 2 + 2, 2 + 1 + 1 and 1 + 1 + 1 + 1.

    How many ways can I put n identical balls into k distinct boxes?

    C(n + k − 1, n), by stars and bars: line up the n balls as stars and place k − 1 bars to split them into k boxes. 10 identical sweets for 3 children can be shared in C(12, 2) = 66 ways; if every child must get at least one, C(9, 2) = 36.

    Ready to try it?

    Open PictorX in your browser — no install, no credit card required to start.

    Go to Editor