Maths & numbers

Permutation rank and unrank calculator with repeated elements

Rank and unrank connect a multiset arrangement with its position in a precisely declared lexicographic order.

Your numbers

Ranks start at zero.
Space/comma-separated labels; repeated labels are indistinguishable.
List every distinct multiset symbol once. Order is exactly as entered.
Must use exactly the multiset tokens. Ignored in unrank mode.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Rank and unrank connect a multiset arrangement with its position in a precisely declared lexicographic order. Unlike permutations of distinct labeled objects, exchanging equal tokens does not create another arrangement. The calculator counts prefix-completion blocks exactly and either adds the blocks preceding an entered word or walks those blocks to reconstruct a requested index. You supply the symbol order, so numeric-looking labels are never silently sorted under a different rule.

THE FORMULANumber of words=N!/∏c_s!. For each prefix, skip the completion blocks beginning with smaller available symbols.

A worked example

The multiset A,A,B has three arrangements in the declared order A before B: A A B at rank zero, A B A at rank one, and B A A at rank two. For A B A, the first A skips no earlier block. At the second position, the completion beginning with A is A A B, so one word is skipped before B is selected. The final A finishes rank one.

Good to know

Use 1–50 total tokens and 1–16 distinct symbol labels of at most 16 characters each. Labels are separated by commas or whitespace and cannot contain those separators. The order must contain every distinct multiset label once, and rank mode requires exactly the multiset multiplicities. Ranks are zero-based exact integers. This is not a permutation composition, a Gray-code order or a random shuffle.

Count complete prefix blocks without listing every word

If the remaining multiplicities are c₁,…,c_s, the number of different completions is their total factorial divided by the product of their individual factorials. A hypothetical next token reduces its multiplicity by one before that formula is applied. The resulting block size counts all words sharing that extended prefix, including repeated-token arrangements only once.

Ranking inspects candidate next tokens in the declared order. Every available token smaller than the actual next token contributes its entire completion block to the rank. Once the actual token is selected, its count is reduced permanently and the process moves to the next position. The table records the skipped count at each position and its cumulative total, rather than pretending one increment corresponds to one labeled copy.

Repeated A tokens produce three distinct words and zero-based rank one for ABAA A Brank 0A B Arank 1B A Arank 23! / 2! = 3 distinct words
With symbol order A before B, multiset A,A,B has words AAB, ABA, BAA. The two A copies are indistinguishable, so there are three words rather than six. ABA is the second word, rank one.

The Permutation composition calculator composes already-given permutations; ranking here indexes distinct multiset words in a declared order.

Declare an order rather than relying on how labels look

Labels 2 and 10 do not acquire numeric ordering automatically. If you enter order 2,10, that is the chosen order; if you enter 10,2, the two blocks exchange places. Similarly, uppercase and lowercase labels are distinct unless their text is identical. This avoids locale-dependent results and makes a saved rank reproducible.

A rank belongs to the combination of multiset, symbol order and indexing convention. It is not meaningful by itself. Changing any of those can select a different arrangement even when the same integer is entered. A repeated label in the order list is rejected, as is an order list that omits or invents a symbol. Such inputs would otherwise make the completion-block partition ambiguous.

Unranking reverses the same partition

For unranking, compare the remaining rank with the first available completion block. If the rank is at least that block size, subtract it and move to the next candidate token. Otherwise select that token and recurse within its block. The rank must lie between zero and the total number of distinct arrangements minus one; an out-of-range request cannot be wrapped around silently.

Exact integer arithmetic is necessary because even fifty tokens can have far more arrangements than a floating-point integer can distinguish. The engine never converts the rank to a JavaScript number. A useful independent check is a round trip: rank an arrangement, unrank the returned value using the same multiset and order, and require the original token sequence back.

A few questions, answered

Do equal copies have separate ranks?

No. Only the resulting token word matters. Exchanging two identical copies of A leaves the same arrangement.

Why is rank one the second arrangement?

Ranks start at zero. The first arrangement is rank zero, the second rank one, and the last total minus one.

Find your calculator

Start typing to explore the library.