I am trying to use a recursive function to calculate the fibonacci sequence. This is what I have come up with:
import sys
new_recursion_limit=3000
sys.setrecursionlimit(new_recursion_limit)
fibonacci_cache = {}
def fibonacci(n):
if n in fibonacci_cache:
return fibonacci_cache[n]
if n == 1:
return 1
else:
result = fibonacci(n - 1) + fibonacci(n - 2)
fibonacci_cache[n] = result
return result
number = int(input("Enter the number for fiboonacci value: "))
result = fibonacci(number)
print(f"The value of fibbonaci {number} is {result}")
I used chatGPT so that I can have a basic idea to implement the method. The error I receive is a RecursionError: maximum recursion depth exceeded. even for the int 5 I am receiving this error. I am a beginner in programming and I am trying to implement the recursive function so that I have a proper idea about the functioning. Can anyone please help me with this error?
When I received the error I tried to use sys function to se a recursive limit. Th repetation is 1000+ in this case. I was expecting the recursive function to work normally. but I still receive the error.