0
$\begingroup$

When using two different online Python compilers/interpreters for executing a program (this one, and this one), I found that both sites showed the maximum recursion depth error at $n = 998$, whereas the one which I had downloaded from python.org showed the error at $n = 1024$. Is there a reason why online compilers/interpreters have lower maximum recursion depth limits?

For example, this recursive function (which displays the first $n$ terms in the Fibonacci sequence) shows a recursion error at $n = 998$ in an online compiler/interpreter.

def fib_memo(n):
    if n in fib_cache:
        return fib_cache[n]
    if n == 1:
        value = 1
    elif n == 2:
        value = 1
    elif n > 2:           
        value =  fib_memo(n-1) + fib_memo(n-2)
    fib_cache[n] = value
    return value
fib_cache = {}
print (fib_memo(998))

But it shows a recursion error at $n = 1024$ in the Windows Python application.

$\endgroup$
1
  • 5
    $\begingroup$ The recursion depth is platform dependent. You can't make any assumption on what it will be across platforms. You can read the current limit with sys.getrecursionlimit(), and try to set it with sys.setrecursionlimit(). $\endgroup$ – Steven Feb 28 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.