What is the maximum recursion depth in Python, and how to increase it?

前端 未结 17 2920
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 04:14

I have this tail recursive function here:

def recursive_function(n, sum):
    if n < 1:
        return sum
    else:
        return recursive_function(n-1         


        
17条回答
  •  心在旅途
    2020-11-21 05:04

    I realize this is an old question but for those reading, I would recommend against using recursion for problems such as this - lists are much faster and avoid recursion entirely. I would implement this as:

    def fibonacci(n):
        f = [0,1,1]
        for i in xrange(3,n):
            f.append(f[i-1] + f[i-2])
        return 'The %.0fth fibonacci number is: %.0f' % (n,f[-1])
    

    (Use n+1 in xrange if you start counting your fibonacci sequence from 0 instead of 1.)

提交回复
热议问题