00.斐波那契数列第n项

# 斐波那契数列第n项
# 1 1 2 3 5 8


def fib(n):
    if n <= 2:
        return 1
    else:
        return fib(n-2)+fib(n-1)


def fib2(n):
    if n < 3:
        return 1
    f1 = f2 = 1
    for k in range(1, n-1):
        f1, f2 = f2, f2+f1
    return f2


if __name__ == '__main__':
    # 1 1 2 3 5 8 13
    print(fib(6))  # 8
    print(fib2(6))  # 8

 

posted @ 2019-10-15 14:17  fly_bk  阅读(175)  评论(0编辑  收藏  举报