Skip to content

Instantly share code, notes, and snippets.

@voith
Last active November 12, 2019 04:20
Show Gist options
  • Select an option

  • Save voith/4fc7e8c2ab06bcb1e9e02f308c7b686c to your computer and use it in GitHub Desktop.

Select an option

Save voith/4fc7e8c2ab06bcb1e9e02f308c7b686c to your computer and use it in GitHub Desktop.
millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9
from math import factorial
numbers = list(range(10))
nth_perm = 1000000
total_numbers = len(numbers)
num = ''
def get_factorial_divisor(factorial, dividend):
quotient = dividend // factorial
remainder = dividend % factorial
if remainder == 0:
quotient -= 1
remainder = dividend % (quotient * factorial)
return quotient, remainder
for i in range(total_numbers - 1, 0, -1):
ifactorial = factorial(i)
quotient, nth_perm = get_factorial_divisor(ifactorial, nth_perm)
num += str(numbers.pop(quotient))
num += str(numbers[0])
print(num)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment