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)

 

    原文作者:撒欢
    原文地址: https://www.cnblogs.com/frisk/p/11626066.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞