编程语言
首页 > 编程语言> > Python实现斐波那契递归和尾递归计算

Python实现斐波那契递归和尾递归计算

作者:互联网

##斐波那契递归测试
def fibonacciRecursive(deepth):
    if deepth == 1:
        return 1
    elif deepth == 2:
        return 1
    else:
        return fibonacciRecursive(deepth - 1) + fibonacciRecursive(deepth - 2)

##斐波那契尾递归测试
def fibonacciTailRecursive(num, ret1, rte2):
    if num == 1:
        return rte2
    return fibonacciTailRecursive(num-1, rte2, ret1+rte2)

if __name__ == "__main__":
    a = fibonacciRecursive(30)
    print(a)
    a = fibonacciTailRecursive(30, 0, 1)
    print(a)

 

标签:__,deepth,fibonacciRecursive,return,Python,斐波,num,递归计算,rte2
来源: https://www.cnblogs.com/frisk/p/11626066.html