Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, you don't need to search through all possible alternatives. A simple recursive algorithm (like the one given by @recursive) will give you the answer. If you are looking for a function that actually outputs all of the combinations, rather than just how many, here is a version written in R. I don't know what language you are working in, but it should be pretty straightforward to translate this into anything, although the code might be longer, since R is good at this kind of thing.</p> <pre><code>allCombos&lt;-function(len, ## number of items to sample x, ## array of quantities of balls, by color names=1:length(x) ## names of the colors (defaults to "1","2",...) ){ if(length(x)==0) return(c()) r&lt;-c() for(i in max(0,len-sum(x[-1])):min(x[1],len)) r&lt;-rbind(r,cbind(i,allCombos(len-i,x[-1]))) colnames(r)&lt;-names r } </code></pre> <p>Here's the output:</p> <pre><code>&gt; allCombos(3,c(3,3),c("white","black")) white black [1,] 0 3 [2,] 1 2 [3,] 2 1 [4,] 3 0 &gt; allCombos(10,c(15,1,1,1,1,1),c("white","black","blue","red","yellow","green")) white black blue red yellow green [1,] 5 1 1 1 1 1 [2,] 6 0 1 1 1 1 [3,] 6 1 0 1 1 1 [4,] 6 1 1 0 1 1 [5,] 6 1 1 1 0 1 [6,] 6 1 1 1 1 0 [7,] 7 0 0 1 1 1 [8,] 7 0 1 0 1 1 [9,] 7 0 1 1 0 1 [10,] 7 0 1 1 1 0 [11,] 7 1 0 0 1 1 [12,] 7 1 0 1 0 1 [13,] 7 1 0 1 1 0 [14,] 7 1 1 0 0 1 [15,] 7 1 1 0 1 0 [16,] 7 1 1 1 0 0 [17,] 8 0 0 0 1 1 [18,] 8 0 0 1 0 1 [19,] 8 0 0 1 1 0 [20,] 8 0 1 0 0 1 [21,] 8 0 1 0 1 0 [22,] 8 0 1 1 0 0 [23,] 8 1 0 0 0 1 [24,] 8 1 0 0 1 0 [25,] 8 1 0 1 0 0 [26,] 8 1 1 0 0 0 [27,] 9 0 0 0 0 1 [28,] 9 0 0 0 1 0 [29,] 9 0 0 1 0 0 [30,] 9 0 1 0 0 0 [31,] 9 1 0 0 0 0 [32,] 10 0 0 0 0 0 &gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload