当前位置:网站首页>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 :
边栏推荐
- 升级 TiDB Operator
- "Friendship and righteousness" of the center for national economy and information technology: China's friendship wine - the "unparalleled loyalty and righteousness" of the solidarity group released th
- Step by step guide to setting NFT as an ens profile Avatar
- Image fusion -- challenges, opportunities and Countermeasures
- Day29-t77 & t1726-2022-02-13-don't answer by yourself
- [Yugong series] February 2022 U3D full stack class 010 prefabricated parts
- CAD ARX 获取当前的视口设置
- 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
- C语言自定义类型:结构体
- [t31zl intelligent video application processor data]
猜你喜欢
面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
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
"Designer universe" Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers | national economic and Informa
Golang DNS write casually
化不掉的钟薛高,逃不出网红产品的生命周期
Résumé des diagrammes de description des broches de la série ESP
NFT smart contract release, blind box, public offering technology practice -- jigsaw puzzle
ESP系列引脚說明圖匯總
Nft智能合约发行,盲盒,公开发售技术实战--合约篇
[research materials] 2021 live broadcast annual data report of e-commerce - Download attached
随机推荐
Upgrade tidb with tiup
Asia Pacific Financial Media | female pattern ladyvision: forced the hotel to upgrade security. The drunk woman died in the guest room, and the hotel was sentenced not to pay compensation | APEC secur
JS select all and tab bar switching, simple comments
Secure captcha (unsafe verification code) of DVWA range
Database basic commands
[research materials] 2022 China yuancosmos white paper - Download attached
1202 character lookup
A Closer Look at How Fine-tuning Changes BERT
CAD ARX 获取当前的视口设置
Onie supports pice hard disk
【云原生】手把手教你搭建ferry开源工单系统
Easy to use tcp-udp_ Debug tool download and use
Mex related learning
File upload of DVWA range
数据治理:误区梳理篇
The Vice Minister of the Ministry of industry and information technology of "APEC industry +" of the national economic and information technology center led a team to Sichuan to investigate the operat
【T31ZL智能视频应用处理器资料】
Circular reference of ES6 module
flask返回文件下载
面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读