I have a problem that can be modeled as:

there are 8 boxes in total, and 60 different items. I want to put all items into boxes (all of them can be put into a single box).

Now I want to find:

1) the number of total posssible situations

2) the number of posssible situations when certain number of items are put into certain box (such are 6 different items are put into box #2).


I found a somehow similar problem, but I think my data would be too large to enumerate. https://stackoverflow.com/questions/20721161/combinations-between-two-lists

  • 2
    $\begingroup$ Is this a Mathematica related question, or did you mean to post this on MATH.stackexchange.com? $\endgroup$
    – ciao
    Mar 5, 2014 at 9:34
  • $\begingroup$ I want to use mathematica to solve this problem, but I have no clue about where to start with. $\endgroup$ Mar 5, 2014 at 9:37
  • $\begingroup$ I think if you only want to know the number (instead of figuring out explicit elements), it's indeed a math problem. $\endgroup$
    – Yi Wang
    Mar 5, 2014 at 10:09
  • $\begingroup$ 6 in box 2: 60!/54! 54^7.. mathematica will crunch the numbers but you need to work out the math on your own. (this problem is far to large for direct simulation) $\endgroup$
    – george2079
    Mar 5, 2014 at 13:25
  • $\begingroup$ bzzt, guilty of commenting before coffee again. I'll post an answer with the correct result $\endgroup$
    – george2079
    Mar 5, 2014 at 15:04

2 Answers 2


I find it useful to work through the sort of problem with a smaller example:

 nbox = 4;
 nball = 5;

generate all ball-to-box binnings:

 boxassign = Tuples[Range[nbox], {nball}];
 boxassign[[;; 10]] // MatrixForm

enter image description here

the formula for the total count is pretty obvious i think:

 (boxassign // Length ) == nbox^nball


Now transpose that to a list of the balls in each box: (I'm sure there are more efficient ways to do this, but this is clearly readable)

 ballinbox = 
     Function[box, Sort@Flatten@Position[#, box]] /@ Range[nbox] & /@ boxassign;
 ballinbox[[1 ;; 10]] // MatrixForm

enter image description here

Now select the cases where there are three balls in box two for example:

  nintwo = 3;
 ( byintwo = Select[ ballinbox , Length[#[[2]]] == nintwo & ] ) // MatrixForm

enter image description here

you can inspect the unique sets in box two and observe that it is all the possible subsets:

 Union@(byintwo[[ ;; , 2]])
 Length[%]   ==  nball! / nintwo! / (nball - nintwo)!


Now the total count is just that number multiplied be all the ways we can distribute the remaining balls:

Length[byintwo] == 
     ( nball! / nintwo! / (nball - nintwo)! ) (nbox - 1)^(nball - nintwo)


so that for the example:

 Clear[nball,nintwo,nbox]; (* oops!! *)
 ( nball! / nintwo! / (nball - nintwo)! ) (nbox - 1)^(nball - nintwo) /.
      {nball -> 60, nintwo -> 6, nbox -> 8}


  • $\begingroup$ Unfortunately: $\frac{60!}{6!54!} 7^{54}=216181483166095764640743948702606019893283529061497140\neq 262440$ $\endgroup$
    – ubpdqn
    Mar 6, 2014 at 3:34
  • $\begingroup$ DOH! @ubpdqn i will fix that, good catch $\endgroup$
    – george2079
    Mar 6, 2014 at 12:34




I am viewing this as a labeled balls into labeled urns because all 60 in the last urn is different then all 60 in the first urn.

I have a little function for computing these:


where distballs = 60, disturns = 8, occupied urns = 1 to 8 because there can be 0 urns empty or 7 urns empty.




A fair amount of assumptions were made but that is how I would do it. I do agree that the question belongs on Mathematics.SE.

  • 2
    $\begingroup$ For the first item you have 8 possibilities, for the second item you have 8 possibilities, for the third item you have 8 possibilities... err... etc. 8^60 $\endgroup$ Mar 5, 2014 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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