The Challenge
For a given set of n integers, write a program which will output its lexicographic index.
The Rules
- The input must only be a set of unique non-negative integers separated by spaces.
- You should output the lexicographic index (range 0 to n!-1 inclusive) of the permutation.
- No permutation libraries or permutation built-ins may be used.
- You may not generate the set of permutations or any subset of permutations of the input to help you find the index.
- You also can't increment or decrement the given permutation to the next/previous(lexicographically) permutation.
- Bonus points (-10 bytes) if you find some way to complete this without using factorials.
- Runtime should be less than 1 minute for n = 100
- Shortest code by byte count wins
- Winner chosen Tuesday (July 22, 2014)
More About Permutations
- http://www.monkeyphysics.com/articles/read/26/numbering_permutations.html
- Permutation group operation
- http://lin-ear-th-inking.blogspot.com/2012/11/enumerating-permutations-using.html
Examples
0 1 2 --> 0
0 2 1 --> 1
1 0 2 --> 2
1 2 0 --> 3
2 0 1 --> 4
2 1 0 --> 5
0 1 2 3 4 5 6 7 --> 0
0 1 2 3 4 5 7 6 --> 1
0 1 2 3 4 6 5 7 --> 2
1 3 5 17 --> 0
781 780 779 13 --> 23
81 62 19 12 11 8 2 0 --> 40319
195 124 719 1 51 6 3 --> 4181