Python实现斐波那契

昨天我写了一版JavaScript实现的,今天用Python实现一下

什么是斐波那契

斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368........
这个数列从第3项开始,每一项都等于前两项之和

代码实现

arr = []

def fibonacci():
  i = 0
  l = len(arr)
  if l < 2:
    i = 1
  else:
    i = arr[l - 1] + arr[l - 2]
  arr.append(i)

  return i

for i in range(10):
  print(fibonacci())

Output:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55

分享

TITLE: Python实现斐波那契

LINK: https://www.qttc.net/258-python-fibonacci.html

NOTE: 原创内容,转载请注明出自琼台博客