0

When I try

Permutations[Range[1, 12]]; // AbsoluteTiming

I get

{53.6949, Null}

and with Permutations[Range[1, 14]]; // AbsoluteTiming the computer takes hours.

Is this true in general, or is it a slow function in Mathematica?

Is there any very fast way of getting the permutations of the list $ \{1, 2, \dots, n\} $? Perhaps with parallel processing?

apg
  • 2,075
  • 10
  • 13
  • It grows like a factorial, which gets big really quickly. Look at Length[Permutations[Range[1, #]]] & /@ Range[10] – bill s Sep 05 '19 at 13:55
  • But so big it takes a fast processor hours to compute? Even up to 14? – apg Sep 05 '19 at 13:56
  • @bills It is not like a factorial --- it just is the factorial. – Αλέξανδρος Ζεγγ Sep 05 '19 at 13:58
  • I would say that is growing a LOT like a factorial. – bill s Sep 05 '19 at 14:00
  • @Alexander Kartun-Giles What do you want to do with this permutation list when you have it? Apart from the problem of storing it (which can be circumvented by looping and generating them on the fly instead), depending on what you want to compute, it might be unrealistic that the computation finishes in a reasonable amount of time. If it's a very simple computation i would suggest the looping and generating in-place approach. – Thies Heidecke Sep 05 '19 at 14:44
  • 4
    "But so big it takes a fast processor hours to compute?" https://en.wikipedia.org/wiki/Combinatorial_explosion Yes. Exponential complexity is like hitting a wall. You can compute it up to n, but not n+1. Maybe you can go to n+1 if you wait 5-10 years for faster computers, but not to n+2. This problem is worse than exponential (factorial) both in computation time and in memory requirements. – Szabolcs Sep 05 '19 at 16:32

2 Answers2

2

You can check the memory usage:

ByteCount[Permutations[Range[11]]]

You see this is 3.5GB. for 12 it will be ~42GB. I guess you have less memory than that. So it will start to do swapping memory to your hard-drive, with all kinds of performance issues.

Make sure to have $HistoryLength = 1 such that not all the outputs are stored for a long time and fill up your memory.

What do you want to do with this huge list?

SHuisman
  • 3,258
  • 8
  • 12
1

It grows like the factorial function. Indeed:

Length[Permutations[Range[1, #]]] & /@ Range[10] == Factorial[Range[10]]

True

So

Factorial[14]
87178291200
bill s
  • 68,936
  • 4
  • 101
  • 191