Skip to main content

function construction - Number by permutation


I want a function that compute number of permutation in lexicographical order. E.g.



NumberByPermutation[{3,2,1}] = 6 
NumberByPermutation[{1,2,4,3}] = 2
NumberByPermutation[{1}] = 1

maybe something like that already realesed in Mathematica?



Answer



Just a note - any method generating the permutations and the searching will get very slow very quickly, and blow RAM soon after.


Something like this s/b much more efficient (e.g., on my goof-top, for permutations of length 10, it's ~30,000X faster :


pr[{}] = 1;
pr[{x_, y___}] := Tr[Clip[{y}, {x, x - 1}, {1, 0}] ] Length@{y}! + pr[{y}];

Comments