代码改变世界

Algorithm - Fibonacci

2021-06-23 16:33  WayneWei  阅读(35)  评论(0编辑  收藏  举报

Algorithm implementation Fabonacci:

# Fabonacci: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

def recursion(n):
    if n == 1 or n == 2:
        return 1
    else:
        return recursion(n-1) + recursion(n-2)

def fabonacci(n):
    for i in range(n):
        print(recursion(i+1), end=' ')

fabonacci(7)

Testing output:

D:\test\venv\Scripts\python.exe D:/test/algorithm.py
1 1 2 3 5 8 13 
Process finished with exit code 0