# How can I revise my program to avoid “No more memory available”? [duplicate]

I have a program for calculating a type of list.

ff[num_Integer] :=
Union[
Select[Subsets[Tuples[Range[2 num], {2}], {num}],
Sort @ Flatten @ # == Sort @ Range[2 num] &],
SameTest -> (Sort[Sort /@ #1] == Sort[Sort /@ #2] &)];


Now, the problem is that ff[num_] doesn't work on my computer when num >= 5. I would really like to get ff, even ff. How can I change this program to use a better method for producing these types of list?

• Have you tried analyzing your problem on paper before asking Mathematica to crunch the numbers? For just $n=6$, you have roughly $\mathcal{O}(10^{10})$ elements, each containing 6 pairs... You can work out the memory required for that (and see how it becomes astronomically high for $n=10$). See this related question (possible duplicate) – rm -rf Jun 15 '13 at 7:29
• All the solutions consist of a list beginning with 1, followed by permutations of Range[2,2n], such that the odd elements of the full lis increase monotonically. This suggests an approach of producing the odd elements first (always beginning with 1), and then Riffle-ing with all permutations of the remaining elements, which are the Complement[Range[2, 2n], odd elements] . I have to run so someone else can implement this if they wish. – DavidC Jun 15 '13 at 10:05
• I agree @Simon Woods. But as rm -rf mentioned, Orders will have also with this nice solution unavoidable computational problems. The Length of the output list can be calculated with Product[2*i-1,{i,1,num}]. For num=10 the length would be 654729075. At least this is for sure too much for my computer. – partial81 Jun 15 '13 at 20:24