Problem 104 - Pandigital Fibonacci Ends
This was a really interesting problem, which made me learn more about the efficiency within Python. Using ‘sorted’ was far more efficient than using ‘set’, using the integer part of the log of the number was a neat way to help find the first 9 digits of a number, and the nested If statements were quicker than pairing them with an AND statement (if the kth number doesn’t meet the first criteria, there is no point checking the 2nd criteria.)
import math
def NineDigitCheck(n):
return sorted(str(n)) == ['1','2','3','4','5','6','7','8','9']
def GetFirstNine(n):
num_digits = int(math.log10(n)) + 1
if num_digits > 9:
return int(n / 10**(num_digits - 9))
else:
return n
def Findk():
a, b = 1, 1
k = 2
while True:
a, b = b, a + b
k += 1
if NineDigitCheck(b % 10**9):
if NineDigitCheck(GetFirstNine(b)):
return k
print(Findk())
Gives an output of 329,468