Is there a tool to generate all combinations $C$ where $k=7$, $n=36$?

187 Views Asked by At

I need a tool to generate a list of all combinations (without repetition) of $k=7$ elements in a group of $n=36$ elements (it is over $8$ mln unique combinations).

There is this, but it has 2000 combinations limit.

I think there should be something of the kind in Matlab, Sage, Maxima, Maple, Mathematica, or some other special utility (Scientific Linux, other specialized packages). Maybe someone can give a hint what tool to use?

More specifically, I need not only to generate those 7-out-of-36 combinations: I need to divide (partition) 36 numbers into 8+ mln of 7-element-sets ans 29-element-sets (remaining). Subsets shall have only natural numbers in range 1..36. Numbers never repeat (within each subset), intersection of 7-partition and 29-partition shall be always empty.

4

There are 4 best solutions below

5
On BEST ANSWER

This is not really a math question, but still, here is one of many possible solutions (Mathematica):

a = Subsets[Range[36], {7}];
Export["out.tmp", a, "List"]

Admittedly, this is going to take a while (~4 minutes in my case).

0
On

It is easy enough in R. For example

sevenfrom36 <- combn(36, 7)

will give you a matrix with $7$ rows and $8347680$ columns in about $5$ seconds on my machine, with each column being a combination.

The first ten columns are not very exciting

> sevenfrom36[,1:10]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    1    1    1    1    1    1    1    1     1
[2,]    2    2    2    2    2    2    2    2    2     2
[3,]    3    3    3    3    3    3    3    3    3     3
[4,]    4    4    4    4    4    4    4    4    4     4
[5,]    5    5    5    5    5    5    5    5    5     5
[6,]    6    6    6    6    6    6    6    6    6     6
[7,]    7    8    9   10   11   12   13   14   15    16
0
On

Perhaps it is useful to note that a system like GAP has a command called

IteratorOfCombinations

that allows you to loop over your combinations without having to write out the (longish) list.

0
On

For Matlab/Octave: the command nchoosek(36, 7) computes the number $\tbinom{36}{7}$, while nchoosek(1:36, 7) generates a $\tbinom{36}{7} \times 7$ matrix whose rows are all the combinations (without repetitions) of $7$ elements in the group of numbers $1, 2, \ldots, 36$. If you instead want the combinations of another group of numbers, then you can encode them in a vector x and invoke nchoosek(x, 7) (if you also want the combinations to be non-decreasing, then make sure the numbers in x are already non-decreasing.)

If you want to do stuff over each combination, then you can use a for cycle as follows:

for j = nchoosek(1:36, 7)',
    % Do stuff over j' (row form) or over j (column form)
end