实现斐波那契数列(兔子序列)

源码1:

# !/usr/bin/python3


# 定义斐波那契数列计算函数
def fib(n):
    if n == 1 or n == 2:
        return 1
    return fib(n-1) + fib(n-2)

# 定义打印函数
def printFib(n):
    num = range(1,n)
    for x in num:
        if x < len(num):
            print(fib(x),end=',')
        else:
            print(fib(x))

printFib(30)

输出结果

1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229