Take the 2-minute tour ×

I am creating all possible permutations of a set, say {1,2,3}, given that I can choose two numbers every time. I understand that this can be done using permutations function but my list can be very big and after creation of such a huge matrix it would take too long to do any operations on it. Hence, I wrote the following recursive function that does what I want:

myset = {1, 2, 3};
numOfBins = 2;
rec[x_] := (
    x <= numOfBins,
     h[[x]] = j; 
     rec[x + 1],
     {j, 1, Length[myset]}

The outcome of the this code is:


Now I would like to know how can I do this using functional programming maybe with Nest or NestWhile...

share|improve this question
Wouldn't the matrix be same huge regardless of how you make it ? –  b.gatessucks Nov 1 '12 at 15:18
I am not a pro but some how using the recursive function is faster. –  a.a Nov 1 '12 at 19:52

1 Answer 1

If you request only those permutations of length 2, Mathematica can return the result rather quickly.

AbsoluteTiming[Permutations[Range[500], {2}]]

permutations of length2

Mathematica does not first generate all permutations and then select those of length 2. It cannot even handle the task of finding all permutations of a list of 500 items.


all permutations

share|improve this answer
well my problem is not limited to two, it may increase to 7 or 10 and this is a procedure that can repeat as well. –  a.a Nov 3 '12 at 8:32
I see. Let me think a bit a bit about this. –  David Carraher Nov 3 '12 at 11:21

Your Answer


