当前位置:网站首页>Recursive method | Fibonacci sequence
Recursive method | Fibonacci sequence
2022-07-27 12:15:00 【Canghai light boat 690】
Recursive method :
Simplify the problem , Step-by-step , One by one 、 Until it can be solved , A step you can take . Finally, it turns into a simple problem .
It can call itself directly or indirectly :
Call directly or indirectly sum() Oneself :
python:
In[1]
def listsum(a):
if len(a) == 1:
return a[0] # If there is only one element in the list , Then return to a[0]
else:
return a[0] + listsum(a[1:]) # Returns the sum of the first element and the remaining other elements
print(listsum([1,3,5,7,9,13]))
Out[2]:
38
Introduction to Fibonacci sequence :
Fibonacci sequence , also called ‘ Rabbit Series / Golden section series ’.
Characteristic 1 :
Any number is the sum of the first two numbers .
for example :
So the first way to calculate Fibonacci sequence , That is to add the last two elements of the number sequence , Get a new number and insert it at the end of the sequence .
Characteristic 2 :
In the limit condition , The quotient of two adjacent elements is equal to a constant . namely
The first one is :
So we can set an array .
a = [0,1]
The next data to insert is “a【-1】+a【-2】”.
a.append(a[-1] + a[-2])
Continue to cycle . loop y Once you get y A new number .
Finally, the number of numbers in the Fibonacci sequence is n = y + 2 .
According to the number of Fibonacci numbers the user wants n To define the number of cycles y.
y = n - 2
Input 【1】:
def fibs1(n): # Define the Fibonacci function a = [0] # Statement a Is an array if n <= 0: print(' error ') #n You can't <= 0 else: if n > 1: a = [0,1] for i in range(n - 2): # perform n-2 Secondary cycle , Immediate array a Newly added n-2 A Fibonacci number a.append(a[-2] + a[-1]) # New figures = The sum of the last two numbers return a
Input 【2】:
fibs1(10)
Output 【2】:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
The second kind :
Define the number of elements in the array in advance , Then set each value as the sum of the first two numbers .
Input 【1】:
def fibs2(n): #n For the number of Fibonacci numbers needed f = [0] * n # The definition contains n individual 0 Array of if n <= 0: print(' error ') #n You can't <= 0 else: if n >= 2: f[1] =1 # here f = [0 , 1] for i in range(2,n): f[i] = f[i - 1] +f[i-2] ''' f[2] = f[1] + f[0] = 1, here f = [0,1,1]; Three Fibonacci numbers f[3] = f[2] + f[1] = 2, here f = [0,1,1,2]; Four Fibonacci numbers ...... f[n-1] = f[n-2] + f[n-1];n A Fibonacci number ''' return f
Input 【2】:
fibs2(10)
Output 【2】:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Comparison of the running time of the two methods :
Input 【1】:
%timeit fibs1(100)%timeit fibs2(100)
Output 【1】:
26.2 µs ± 736 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)30.7 µs ± 301 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
边栏推荐
- [网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
- LNMP架构搭建(部署Discuz论坛)
- Analysis of the use of JUC framework from runnable to callable to futuretask
- Pytorch shows the summary like tensorflow
- Interaction free shell programming
- USB network card drive data stream
- Greek alphabet reading
- shell编程之免交互
- Do you really understand the underlying data structure skip list of Zset in redis?
- Iptables firewall
猜你喜欢

SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series

Solution: can not issue executeupdate() or executelargeupdate() for selections

孤独的年轻人,戒不了Jellycat

Strictly control outdoor operation time! Foshan housing and Urban Rural Development Bureau issued a document: strengthening construction safety management during high temperature
![[machine learning whiteboard derivation series] learning notes - support vector machine and principal component analysis](/img/54/dc232f737da99c7097c395a326e74f.png)
[machine learning whiteboard derivation series] learning notes - support vector machine and principal component analysis

Makefile template

MySQL数据库主从复制集群原理概念以及搭建流程

我在英国TikTok做直播电商

Shell script text three swordsmen sed

Shell脚本文本三剑客之sed
随机推荐
Shell script text three swordsman awk
While loop instance in shell
Guangdong's finance has taken many measures to help stabilize the "ballast stone" of food security
Difference quotient approximation of wechat quotient
[网摘][医学影像] 常用的DICOM缩略图解释以及Viewer converter 转换工具
Do you really understand the underlying data structure skip list of Zset in redis?
mysql8msi安装教程(数据库mysql安装教程)
The first case of monkeypox in pregnant women in the United States: the newborn was injected with immunoglobulin and was safely born
关于离线缓存Application Cache /使用 manifest文件缓存
Regular expression of shell programming (grep of shell script text three swordsmen)
Leetcode 04: T26. Delete duplicate items in the sorting array (simple); Sword finger offer 67. convert the string to an integer (medium); Interview question 01.08. zero matrix (simple)
[machine learning whiteboard derivation series] learning notes - probability graph model and exponential family distribution
孤独的年轻人,戒不了Jellycat
TapNet: Multivariate Time Series Classification with Attentional Prototypical Network
Shake quickly to rescue the "frustrated person"
Leetcode 03: t58. Length of the last word (simple); Sword finger offer 05. replace spaces (simple); Sword finger offer 58 - ii Rotate string left (simple)
Makefile template
CLS 监控告警:实时保障线上服务高可用性
Mysql8msi installation tutorial (database mysql installation tutorial)
Shell编程之正则表达式(Shell脚本文本三剑客之grep)