I was trying Project Euler problem 25 on HackerRank.
I attempted a brute force method using Binet's Formula.
import math
for _ in range(int(input())):
numLen = int(input())
for x in range(1, 5000):
fib = 1/pow(5,.5)*(pow(((1+pow(5,.5))/2),x) - pow(((1-pow(5,.5))/2),x))
if(numLen == len(str(int(fib)))):
print(x)
break
Here 5000 is an arbitrary number based on the assumption input is not more than that and the problem is not here as its a runtime error which I'm think is because it's exceeding integer range(not sure).
It also calculates other test cases in less than 0.25 seconds.
I did some searching and found this.
but by that recurrence method:
import math
for _ in range(int(input())):
a = 1
b = 0
n = int(input())
term = 1
while len(str(a)) != n:
a, b = a+b, a
term+=1
print (term)
gives a timeout as it exceeds 10 seconds.
Can you please help me find a way to improve the first method and optimize the second method.
Note: I tried storing the pow() values in variables instead of recomputing and was of not much help.