Finding a particular nth permutation of a number Let's suppose we have a number 123, and we want the exact 3rd permutation of this number. The permutations in order would be {123, 132, 213, 231, 312, 321}; so the answer to our question would be 213.
But suppose we have a number that is much bigger, like 1234567, we cannot simply list out all the permutations and count to which one we want.
Is there any way to MATHEMATICALLY determine what the nth permutation of a number is?
I have tried to write some code:
def permute(s, answer):
    if (len(s) == 0):
        print(answer, end = "  ")
        return
     
    for i in range(len(s)):
        ch = s[i]
        left_substr = s[0:i]
        right_substr = s[i + 1:]
        rest = left_substr + right_substr
        permute(rest, answer + ch)

answer = ""
 
s = input("Enter the string : ")
 
print("All possible strings are : ")
permute(s, answer)

However this only shows me all the basic permutations that the number can have, and it doesn't really help me understand the concept.
I have tried to modify the equations for both combination and permutations but they haven't worked either, so any help or guidance would be appreciated.
 A: Notice that, in your example, of the $3!$ permutations of $123$, the first $2!$ of them start with $1$, then the next $2!$ of them start with $2$, and the next $2!$ of them start with $3$. This is true in general: of the $n!$ permutations of $12\cdots n$, we can divide them into $n$ blocks consisting of $(n-1)!$ permutations all starting with the same number.
We can use this to write a recursive function in Python that accomplishes what you're asking:
def nth_permutation(set_of_nums, n):
    length = len(set_of_nums)
    if length == 1: return set_of_nums
    block = math.floor(n/math.factorial(length-1))
    index = n % math.factorial(length-1)
    modified_set = set_of_nums[:n-1] + set_of_nums[n+1:]
    return [set_of_nums[block]].append(nth_permutation(modified_set, index))

Disclaimed: I have not tested this code yet, so it might be buggy. For now just treat it like pseudocode.
A: One approach is to use the factorial number system. If you use inversion notation to represent the permutations they will be listed in lexicographic order as desired. While you do have to convert from decimal to factorial the process is relatively straightforward.
