n = 1
rep = 0
def f(n):
if n == 0:
return 0
if n == 1:
return 1
return f(n - 1) + f(n - 2)
while rep <= 50:
print(f(n))
rep += 1
n += 1
I need to print the Fibonacci number 1~50
But errors occur because of the running time of the code.
Q. How can I fix this code to run faster?
Q. The answer code moves the previous number, F(n-2) to a temporary function and carries the current number, F(n-1) to previous number, F(n-2); which makes those two numbers go back a step when Fibonacci numbers are lined up at a list.
(ignore Q2 if there is something wrong)