Created
March 30, 2022 02:10
-
-
Save lusi1990/f4c56f8cb9a60d16fd1a31a857a91fa9 to your computer and use it in GitHub Desktop.
斐波那契数列
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
生成斐波那契数列的前20个数。 | |
说明:斐波那契数列(Fibonacci sequence),又称黄金分割数列, | |
是意大利数学家莱昂纳多·斐波那契(Leonardoda Fibonacci)在《计算之书》中提出一个 | |
在理想假设条件下兔子成长率的问题而引入的数列,所以这个数列也被戏称为"兔子数列"。 | |
斐波那契数列的特点是数列的前两个数都是1,从第三个数开始,每个数都是它前面两个数的和, | |
形如:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...。斐波那契数列在现代物 | |
理、准晶体结构、化学等领域都有直接的应用。 | |
""" | |
fibon = [1, 1] | |
while len(fibon) < 20: | |
fibon.append(fibon[-1] + fibon[-2]) | |
print(fibon) | |
print(len(fibon)) | |
# 第二种方法 | |
a = 0 | |
b = 1 | |
for i in range(20): | |
a, b = b, a + b | |
print(a, end=' ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment