User Rating 0.0 โ˜…โ˜…โ˜…โ˜…โ˜…
Total Usage 0 times
Enter unique characters in canonical sort order
Integer from 0 to n! − 1
Presets:
Is this tool helpful?

Your feedback helps us improve.

โ˜… โ˜… โ˜… โ˜… โ˜…

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.

lexicographic permutation factoriadic Lehmer code permutation index combinatorics sequence converter

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:

k = nโˆ’1โˆ‘i=0 di โ‹… (n โˆ’ 1 โˆ’ i)!

Each factoriadic digit di is extracted iteratively:

di = k(n โˆ’ 1 โˆ’ i)! mod (n โˆ’ i)

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

DictionaryLength nTotal Permutations n!Index kResulting Sequence
abc360abc
abc365cba
01234567891036288009999992783915460
012345678910362880000123456789
ABCD42414CBAD
ABCD42423DCBA
xyz363yxz
abcdef6720719fedcba
1234551206031245
ACGT4246CAGT
ฮฑฮฒฮณฮดฮต5120119ฮตฮดฮณฮฒฮฑ
ABCDEFGHIJKLMNOPQRSTUVWXYZ264.03 ร— 10260ABCDEFGHIJKLMNOPQRSTUVWXYZ

Frequently Asked Questions

The index k must satisfy 0 โ‰ค k < n!, where n is the number of characters in the dictionary. Index 0 always yields the dictionary in its original order (the lexicographically first permutation), and index n! โˆ’ 1 yields the fully reversed dictionary (the lexicographically last permutation).
Yes. The algorithm treats the dictionary string as the canonical sorted order. If the original ranking was performed with characters sorted alphabetically (e.g., 'ABCD'), you must supply them in the same order. Supplying "DCBA" as the dictionary defines a different canonical order and will produce a different sequence for the same index.
JavaScript's Number type is a 64-bit IEEE 754 float, which loses integer precision beyond 253 (9007199254740992). Since 21! already exceeds this limit (51090942171709440000), any dictionary with more than 20 characters would produce incorrect results with standard arithmetic. BigInt provides arbitrary-precision integers.
The dictionary is interpreted as a sequence of Unicode code points (using the JS string iterator which handles surrogate pairs correctly). Each code point is one symbol. Multi-byte emoji and Greek letters work correctly. However, multi-character tokens like "AA" or "ab" as single symbols are not supported - each character is treated individually.
The tool rejects dictionaries with duplicate characters. Permutations of multisets (e.g., 'AABB') use a different ranking formula based on multinomial coefficients: n! รท (n1! โ‹… n2! โ‹… โ€ฆ). Applying the standard algorithm to duplicates would silently produce wrong results.
The factoriadic (factorial number system) represents any integer as a sum of multiples of factorials: k &equals; d1 โ‹… (nโˆ’1)! &plus; d2 โ‹… (nโˆ’2)! &plus; โ€ฆ The sequence of digits di is precisely the Lehmer code of the permutation. Each digit tells you which element to pick from the remaining available symbols at that position.