程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Solving Fibonacci sequence with Python recursive algorithm

編輯:Python

Hello everyone , I meet you again , I'm your friend, Quan Jun .

**

Python Recursive algorithm to solve Fibonacci sequence

**

The Fibonacci sequence refers to a sequence that looks like this 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368…

This sequence starts at number one 3 A start , Each of these terms is equal to the sum of the first two terms .

  • A recursive algorithm Definition : Is a method that a function calls itself directly or indirectly , He usually treats a large complex problem decompose Solve the problem in smaller scale similar to the original problem .

In the computer , First, press all the parameters of the process to the bottom of the stack , Subroutine calls , Finally, get the parameters at the bottom of the stack

def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
print([fibonacci(x) for x in range(10)]

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/150693.html Link to the original text :https://javaforall.cn


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved