Python 实现斐波那契数列中的前50个

简介: Python 实现斐波那契数列中的前50个

斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,斐波那契数列以如下被以递推的方法定义:F(1)=1,F(2)=1, F(n)=F(n-1)+F(n-2)(n>=3,n∈N*)


2.jpeg


代码如下:


def fibonacci(n):
    i,n1,n2 = 0,1,1
    while i < n:
        yield n1
        n1,n2 = n2,n1+n2
        i+=1
res = fibonacci(50)
fi = [i for i in res]
print(fi)

结果如下:

[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, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025]
相关文章
|
2月前
|
Python
Python实现递归的方式来生成斐波那契数列
Python实现递归的方式来生成斐波那契数列
|
1月前
|
Python
【Python 训练营】N_5 斐波那契数列
【Python 训练营】N_5 斐波那契数列
17 2
|
2月前
|
Python
python实现斐波那契数列案例代码
python实现斐波那契数列递归实现斐波那契数列:
20 0
|
2月前
|
存储 Python
Python写斐波那契数列
Python写斐波那契数列
17 0
|
10月前
|
算法 Python
python实现斐波那契数列的多种方式
python实现斐波那契数列的多种方式
|
2月前
|
存储 Python
用 Python 实现斐波那契数列。
【2月更文挑战第9天】【2月更文挑战第25篇】用 Python 实现斐波那契数列。
|
Python
Python实现斐波那契数列
Python实现斐波那契数列
258 0
|
Python
Python列表实现斐波那契数列
Python列表实现斐波那契数列
119 0
Python-剑指offer(7,8,9)斐波那契数列,跳台阶,变态跳台阶
Python-剑指offer(7,8,9)斐波那契数列,跳台阶,变态跳台阶
|
Python
Python 改进斐波那契数列递归后,计算第1000万项只需4秒
Python 改进斐波那契数列递归后,计算第1000万项只需4秒
142 0

相关实验场景

更多