Pandigital Fibonacci ends
Problem 104
The Fibonacci sequence is defined by the recurrence relation:
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
It turns out that F541, which contains 113 digits, is the first Fibonacci number for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9, but not necessarily in order). And F2749, which contains 575 digits, is the first Fibonacci number for which the first nine digits are 1-9 pandigital.
Given that Fk is the first Fibonacci number for which the first nine digits AND the last nine digits are 1-9 pandigital, find k.
from tools import fib_gen_mod, is_pandigital, is_pandigital_end, is_pandigital_start
def end_pandigital():
k = 1
for n in fib_gen_mod():
if is_pandigital_end(str(n)):
yield k
k += 1
def run():
candidates = end_pandigital()
cur = candidates.next()
a = b = 1
k = 3
result = None
while True:
a, b = b, a + b
if k == cur:
v = is_pandigital_start(str(b))
if v:
result = cur
cur = candidates.next()
if v:
break
k += 1
return result