Lexicographic Permutation Index to Sequence Converter
Convert a lexicographic permutation index back to its original sequence using the factoriadic number system with any custom dictionary.
About
Lexicographic permutation ranking assigns every arrangement of n distinct symbols a unique index k in the range [0, n! โ 1]. The forward direction (sequence โ index) is well-known. The reverse - recovering the original sequence from just the index and the dictionary - requires factoriadic decomposition, a procedure where a single arithmetic mistake silently produces a plausible but wrong permutation. This tool implements the exact inverse using arbitrary-precision integer arithmetic, so it handles dictionaries of any practical length without overflow. It assumes the dictionary defines the canonical sorted order; if your ranking used a different ordering, supply the characters in that exact order.
Note: the algorithm assumes all characters in the dictionary are distinct. Repeated characters create a multiset permutation problem with a different ranking formula involving multinomial coefficients. This tool does not handle that case - it validates uniqueness and rejects duplicates.
Formulas
Given a dictionary of n distinct symbols in canonical order and a zero-based index k, the original permutation is recovered by factoriadic (Lehmer code) decomposition:
Each factoriadic digit di is extracted iteratively:
Then k is updated: k โ k mod (n โ 1 โ i)!
Equivalently, at step i, integer-divide k by (n โ 1 โ i)! to get the digit di, then take the remainder as the new k. The digit di is the zero-based index into the remaining available characters. Remove the selected character and proceed.
Where: k = zero-based permutation index, n = dictionary length, di = i-th factoriadic digit (Lehmer code element), (m)! = factorial of m.
Reference Data
| Dictionary | Length n | Total Permutations n! | Index k | Resulting Sequence |
|---|---|---|---|---|
| abc | 3 | 6 | 0 | abc |
| abc | 3 | 6 | 5 | cba |
| 0123456789 | 10 | 3628800 | 999999 | 2783915460 |
| 0123456789 | 10 | 3628800 | 0 | 0123456789 |
| ABCD | 4 | 24 | 14 | CBAD |
| ABCD | 4 | 24 | 23 | DCBA |
| xyz | 3 | 6 | 3 | yxz |
| abcdef | 6 | 720 | 719 | fedcba |
| 12345 | 5 | 120 | 60 | 31245 |
| ACGT | 4 | 24 | 6 | CAGT |
| ฮฑฮฒฮณฮดฮต | 5 | 120 | 119 | ฮตฮดฮณฮฒฮฑ |
| ABCDEFGHIJKLMNOPQRSTUVWXYZ | 26 | ≈ 4.03 ร 1026 | 0 | ABCDEFGHIJKLMNOPQRSTUVWXYZ |