Project Euler 24 - Lexicographic Permutations

Official link: https://projecteuler.net/problem=24

Thought Process

I did this one 2 different ways

  1. Cheating way:

    • Using itertools module this problem is reduced to a one-liner

    • list(itertools.permutations([x for x in range(0,10)], 10))[999999]

  2. Using your brain way:

    • We can sort of count permutations, we know the first permutation is 0123456789, and we know that for the first 9! permutations 0 will be the first digit, so when looking for the millionth permutation our first digit cannot be 0

    • Can the first digit be 1? no, because 2*9! < 1,000,000, but 3*9! > 1,000,000 this means our first digit is 2

    • Same applies for our second digit 2*9! + 6*8! < 1,000,000 but 2*9! + 7*8! > 1,000,000 we have the elements [0,1,3,4,5,6,7,8,9] left so we take the 6th index which is 7 and so on

Interactive Code

Input an integer (yourinput)

Code will output the yourinput-th permutation