def make_permutation(n): p = [1] for i in range(2, n): p = [p[0] * i] + p return p def get_order(n, order): permutation = make_permutation(n) number = list(range(n)) sequence = [] order -= 1 for p in permutation: q = order // p sequence.append(number.pop(q)) order %= p return sequence + number print(''.join(map(lambda x: str(x), get_order(10, 1000000))))