# Calculating nth permutation without repetition efficiently, with variable number of elements

I know I can use the factorial number system to calculate ordered permutations of a set efficiently, given a constant length (for example, [(1, 2, 3), (1, 3, 2), ..., (3, 2, 1)]), but can it be used for non-constant lengths?

For example, my set is {a, b, c}. and the function input/outputs:

f(0) = a
f(1) = b
f(2) = c
f(3) = ab
f(4) = ac
f(5) = ba
...


That is, after the permutations of size 1, there comes permutations of size 2, then size 3, etc., to infinity.

If f(x) = y and the set $$S$$ is {a, b, c}, then given $$x = 999$$:
1. subtract the set size $$3!$$ ($$999 - 3! = 993$$) to "complete" all permutations of size 1
2. repeat this with $$4!$$ ($$993 - 4! = 969$$) to "complete" all permutations of size 2
3. then $$5!$$, $$969 - 5! = 849$$
4. then $$849 - 6! = 129$$
This means that $$999$$ is for a permutation of size 5. You can now use the factorial number system with $$x = 129$$ to calculate ordered permutations of a set of a constant size ($$5$$).