Given the function to generate fibonacci numbers:
def fibonacci(x, memory = {0:0, 1:1}):
if x not in memory:
memory[x] = fibonacci(x-1, memory)+fibonacci(x-2, memory)
return memory[x]
When I try some arbitrarily large number, say 4000th fibonacci number, I get an error:
RuntimeError: maximum recursion depth exceeded
What is the error caused by? What can be done as a work around without sacrificing the efficiency? I prefer this to iteration, since by iterating, calculating even the 50th position takes astronomically long for a computer (read: half a minute).