斐波那契數列的python實現

天飛.h發表於2016-03-15

點選(此處)摺疊或開啟

  1. #!/usr/bin/env python
  2. #* coding:utf8 *
  3. `
  4. 斐波那契數列:初始為0,1,列表中的一個數字是前兩個元素的和
  5. `
  6. num=int(raw_input(`請輸入要計算的斐波那契數列的數目:`))
  7. def fibs(num):
  8.     `文件字串 fibs.__doc__檢視`
  9.     result=[0,1]
  10.     for i in range(num2):
  11.         result.append(result[2]+result[1])
  12.     return result
  13. print fibs(num)

請輸入要計算的斐波那契數列的數目:50
[0, 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]


相關文章