Skip to main content

Euler 0025

The Problem:

What is the millionthfirst lexicographicFibonacci permutationnumber ofwith the1000 digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?digits?

Considerations and Approach:

Well, thisThis is anothersomewhat problemtrivial thatwith Python hassince it can store a simple1000 builtdigit innumber way...

with

Generatingrelative allease, ofand thewe permutationsdon't ofneed theto run a super efficient Fibonacci algorithm or store many numbers 0-9at is not computationally an issue for itertools, we can then sort those permutations and grab out the millionth permutation and that's our answer.all.

The Code:

importfib_1 itertools= #Sorry1
thatfib_2 = 2
digits_to_find = 1000

#Start at index of 4 because we already have 1,1,2
running_count = 4

#keep going until the next fewfib linesdigit don'tlength lookis pythonic,greater Ithan adjustedor themequal for comment clarity sake
#The line should actually just read sorted(itertools.permutations(list(range(10))))[999999]

#We sort
sorted( 
    #allto the permutationsdigit itertools.permutations(to #offind.
thewhile numberslen(str(fib_1 0-9+ list(range(10)fib_2)) )< #anddigits_to_find:
    thenfib we= grabfib_1 out+ thefib_2
    millionthfib_1 permutation= )[999999]fib_2
    fib_2 = fib
    running_count += 1

print(running_count)