Last active
November 12, 2019 04:20
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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