def fib(n):
if n == 1 or n == 2:
return 1
return fib(n-1) + fib(n-2)
for i in range(5):
print(fib(i))
I want to print first 5 result of Fibonacci sequence only to get
RecursionError: maximum recursion depth exceeded in comparison
I think there is an exit of every positive n
and print(fib(4))
, print(fib(20))
and print(fib(100))
works perfectly.
What's wrong with my code?