Lexicographic Permutation Sequence Converter
Convert any permutation to its lexicographic index or find the nth permutation from a custom dictionary. Uses Lehmer code and factoradic system.
About
Lexicographic ordering assigns a unique rank to every permutation of a finite set. The rank is computed via the Lehmer code, which encodes positional displacement against remaining unused symbols. For a permutation of n distinct elements, there exist n! total arrangements. Miscounting a single position cascades through every subsequent factorial weight, producing an entirely wrong index. This tool computes both directions: sequence ā index (0-based) and index ā sequence, using arbitrary-precision BigInt arithmetic so it handles dictionaries well beyond 20 symbols where 20! ≈ 2.43 Ć 1018 exceeds standard floating-point range.
The tool assumes all characters in the input sequence are distinct members of the supplied dictionary. Repeated characters violate the permutation constraint and are rejected. Dictionary order defines the lexicographic baseline. Changing the dictionary order changes the rank. Pro tip: for numeric systems (base-n positional), supply digits in ascending order as your dictionary.
Formulas
The lexicographic rank of a permutation Ļ over n distinct symbols is computed via the Lehmer code:
where Li is the number of symbols in the remaining (unused) set that are lexicographically smaller than Ļi. The inverse operation recovers the permutation from an index:
where r is the running remainder initialized to the target index. At each step, the Li-th smallest remaining symbol is selected and removed from the available set.
Where: Ļ = input permutation, n = dictionary size (number of distinct symbols), Li = Lehmer code digit at position i, r = remainder during index decomposition, i = position index from 0 to nā1.
Reference Data
| Dictionary Size n | Total Permutations n! | Bits Required | Digits in Index | Example Dictionary |
|---|---|---|---|---|
| 3 | 6 | 3 | 1 | A B C |
| 4 | 24 | 5 | 2 | 1 2 3 4 |
| 5 | 120 | 7 | 3 | a b c d e |
| 6 | 720 | 10 | 3 | 0 1 2 3 4 5 |
| 8 | 40,320 | 16 | 5 | A B C D E F G H |
| 10 | 3,628,800 | 22 | 7 | 0 1 2 3 4 5 6 7 8 9 |
| 12 | 479,001,600 | 29 | 9 | A B C D E F G H I J K L |
| 15 | 1,307,674,368,000 | 41 | 13 | a b c d e f g h i j k l m n o |
| 18 | 6,402,373,705,728,000 | 53 | 16 | A through R |
| 20 | 2,432,902,008,176,640,000 | 61 | 19 | a through t |
| 26 | ≈ 4.03 Ć 1026 | 89 | 27 | A through Z (full alphabet) |
| 30 | ≈ 2.65 Ć 1032 | 108 | 33 | Custom 30-symbol set |
| 36 | ≈ 3.72 Ć 1041 | 138 | 42 | 0-9 + A-Z (alphanumeric) |