当前位置:网站首页>Fibonacci sequence
Fibonacci sequence
2022-07-06 08:17:00 【m0_ fifty-two million three hundred and eighty-four thousand ni】
Fibonacci sequence is the most typical and relatively simple recursion , It is relatively simple to solve it with algorithmic thinking .
If you want to write an algorithm, you must first understand what Fibonacci sequence is , Fibonacci sequence (Fibonacci sequence), also called The golden section The sequence , Leonardo the mathematician · Fibonacci (Leonardo Fibonacci) Take rabbit breeding as an example , It is also called “ Rabbit Series ”, It refers to such a sequence :1、1、2、3、5、8、13、21、34、…… In Mathematics , The Fibonacci sequence is defined recursively as follows :F(0)=1,F(1)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 2,n ∈ N*) In Modern Physics 、 accurate Crystal structure 、 Chemistry and so on , Fibonacci sequence has direct application , So , American Mathematics will come from 1963 Published in 《 Fibonacci series Quarterly 》 A mathematical magazine in the name of , Used to publish research results in this field .( Source: Baidu Encyclopedia )
Recursion is a relatively difficult Algorithm , Now I only have some ideas of my own , Recursion is difficult because it has a complex process , Nest layer after layer , I often want to give up learning recursion because it is difficult to imagine the recursive process , But giving up is easy , But persistence must be cool , Learning Fibonacci made me have some ideas about recursion .
Recursion is a nested process layer after layer , My idea is to think only about the conditions for jumping out of the process , And loops that need recursion at first , The condition for Fibonacci sequence to jump out of the cycle is when n <= 2 when , return , If it is greater than 2 when , You need the sum of the first two numbers , Notice that here is the sum of the first two numbers , Instead of the sum of all the previous numbers , Because only the first two numbers need to be considered . These two numbers are not a specific number , All you need to know is how to calculate it , The previous number can be used feiBo(n-1) Instead of , The first two numbers can be used feiBo(n-2) Instead of , So calculate a greater than 2 The number of hours , As long as you need to return feiBo(n-1) + feiBo(n-2) that will do , There is no need to think about the complex recursive process . Of course, for the standardization of the code , It is necessary to judge whether the entered value is reasonable .
The end condition of recursion is :
if(n <= 2){
return 1;
}else{
return feiBo(n-1) + feiBo(n-2);
}
Add constraints and complete the construction as ( The words are not too standard ):
class fei{
public int feiBo(int n){ // Back to a int type
if(n > 0){
if(n <= 2){
return 1;
}else{
return feiBo(n-1) + feiBo(n-2);
}
}else{
return 0;
}
}
}
main The function and all the codes are ( In order to facilitate the constant test directly ):
public class demo17{
public static void main(String[] args) {
fei b = new fei();
int num = b.feiBo(8);
if(num != 0){
System.out.println(num);
}else{
System.out.println(" Incorrect input !");
}
}
}
class fei{
public int feiBo(int n){
if(n>=1){
if(n<=2){
return 1;
}else{
return feiBo(n-1)+feiBo(n-2);
}
}else{
return 0;
}
}
}
The test screenshot is :
边栏推荐
- [research materials] 2021 China online high growth white paper - Download attached
- Circuit breaker: use of hystrix
- 指针进阶---指针数组,数组指针
- NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle
- Migrate data from a tidb cluster to another tidb cluster
- Yu Xia looks at win system kernel -- message mechanism
- Onie supports pice hard disk
- National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
- 远程存储访问授权
- Flash return file download
猜你喜欢
Asia Pacific Financial Media | designer universe | Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers
将 NFT 设置为 ENS 个人资料头像的分步指南
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Pyqt5 development tips - obtain Manhattan distance between coordinates
Easy to use tcp-udp_ Debug tool download and use
Ruffian Heng embedded bimonthly, issue 49
C语言自定义类型:结构体
[research materials] 2021 China online high growth white paper - Download attached
hcip--mpls
Go learning notes (3) basic types and statements (2)
随机推荐
Asia Pacific Financial Media | "APEC industry +" Western Silicon Valley invests 2trillion yuan in Chengdu Chongqing economic circle to catch up with Shanghai | stable strategy industry fund observatio
Migrate data from CSV files to tidb
远程存储访问授权
The resources of underground pipe holes are tight, and the air blowing micro cable is not fragrant?
Database basic commands
华为云OBS文件上传下载工具类
Permutation and combination function
[redis] Introduction to NoSQL database and redis
07- [istio] istio destinationrule (purpose rule)
[research materials] 2022 enterprise wechat Ecosystem Research Report - Download attached
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
Circuit breaker: use of hystrix
Yyds dry goods inventory three JS source code interpretation eventdispatcher
Erc20 token agreement
Mex related learning
你想知道的ArrayList知识都在这
Step by step guide to setting NFT as an ens profile Avatar
Grayscale upgrade tidb operator
从 SQL 文件迁移数据到 TiDB
使用 BR 备份 TiDB 集群数据到兼容 S3 的存储