当前位置:网站首页>[C language] Fibonacci sequence [recursion and iteration]
[C language] Fibonacci sequence [recursion and iteration]
2022-07-28 20:04:00 【An ran_】
One 、 Background introduction
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 ”.
【 Rabbit breeding problem 】
generally speaking , Two months after the rabbit was born , It has the ability to reproduce , A pair of rabbits can give birth to a pair of little rabbits every month . If all the rabbits don't die , How many pairs of rabbits can we breed in a year ?
The following table can be listed by analogy :
Months elapsed 0 1 2 3 4 5 6 7 8 9 10 11 … Number of pups 1 0 1 1 2 3 5 8 13 21 34 55 … The number of Rabbits 0 1 1 2 3 5 8 13 21 34 55 89 Population logarithm 1 1 2 3 5 8 13 21 34 55 89 144 Number of pups = The number of adult rabbits in the previous month
The number of Rabbits = The number of adult rabbits in the previous month + The number of cubs in the previous month
Population logarithm = The number of adult rabbits this month + The number of cubs this month
You can see the number of cubs 、 The number of Rabbits 、 The total logarithms form a sequence . This sequence has very obvious characteristics , That is : The sum of the preceding two adjacent items , Constitutes the latter item .
in short , Here are five points :
(1) Chinese name : Fibonacci sequence
(2) Alias : Golden section series 、 Rabbit Series
(3) English name :Fibonacci sequence
(4) The proposer : leonardo · Fibonacci
(5) expression :F[n]=F[n-1]+Fn-2
Two 、 Thought analysis
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-9yI7PzDR-1651491444162)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220502191554628.png)]](/img/02/6cff776db583f1b149686e15649d41.png)
3、 ... and 、 Code implementation
// Recursive method to solve Fibonacci sequence problem
#include<stdio.h>
int fib(int n)
{
if (n <= 2)
{
return 1;
}
return fib(n - 1) + fib(n - 2);
}
int main()
{
int n;
scanf("%d", &n);
int ret = fib(n);
printf("%d\n", ret);
return 0;
}
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-WqVUglTh-1651491444165)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220502192332650.png)]](/img/0b/83bc590097291009b60fa8a687d58d.png)
Four 、 Expanding reading
【 explain : At present, it is enough to ask for understanding , Just know how to check when you use it . however , At present, in my opinion , It should not be used ...】
1. characteristic
1. Square and antecedents 
【 In particular : Odd and even terms refer to the odd and even number of terms , It's not Index The parity of the numbers of the column itself 】
2. Sum of odd terms
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-k46uF0lg-1651491444168)(https://bkimg.cdn.bcebos.com/formula/305312a50e2f60133b4cba8798c6fab8.svg)]](/img/19/4a0c85ce20d6d4384badf9347d9ba8.png)
3. Sum even terms
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-GaibWqID-1651491444169)(https://bkimg.cdn.bcebos.com/formula/d0870e950a35dc1077f06ef4007183bc.svg)]](/img/f2/a9de4312e95146faa6d0e53b6e1c07.png)
4. Sum of squares
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-bA3Yf3xE-1651491444170)(https://bkimg.cdn.bcebos.com/formula/deeaf9b4277cffe9e620becc06362458.svg)]](/img/10/19ec1d523071d8b643feae099125a1.png)
5. Binomial relationship
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-q6F8OGQG-1651491444172)(https://bkimg.cdn.bcebos.com/formula/af4b93bab863de2e8ee3ecc8134d595a.svg)]](/img/95/364876148f6c4d742e350d220bf5ff.png)
6. Other formulas
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-bjpjCXAs-1651491444173)(https://bkimg.cdn.bcebos.com/formula/797b9d671ab7fc1de8a906f6e0809097.svg)]](/img/4c/639230698ec7ca2b50f4a8435e7802.png)
2. application
1. The golden section
As the number of items in a sequence increases , The ratio of the former to the latter is getting closer The golden section The numerical 0.6180339887……
2. Yang hui triangle

3. Rectangular area
The sum of the squares of the first several items of the Fibonacci sequence can be regarded as squares of different sizes , Because of Fibonacci's recurrence formula , They can be assembled into a large rectangle . So the sum of the areas of all the small squares is equal to the area of the large rectangle . Then we can get the following identity :
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-t6iut1ud-1651491444176)(https://bkimg.cdn.bcebos.com/formula/4e3acee4042a4f988211432f74909cf6.svg)]

4. In nature “ coincidence ”
Two French scientists conducted computer simulation experiments on the process of petal formation , It is proved that when the system keeps the lowest energy , Flowers will grow petals in Fibonacci sequence .
3. Related issues
① There is a flight of stairs with 10 Stepped steps , Stipulate that each step can only span one or two levels , To get on the 10 There are several different steps ?
This is a Fibonacci sequence : There is a way to climb the first step ; Climb two steps , There are two ways to register ; Climb three steps , There are three ways to register ; Climb four steps , There are five ways to register ……
1,2,3,5,8,13…… therefore , Climb level 10 , Yes 89 Seed walking method .
【 explain : Because the problem of jumping steps occurs frequently , So I wrote a separate blog to analyze this problem , Interested partners can move my 【C/C++ Summary of special exercises 】 to glance at 】
② Find the of recursive sequence General formula
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-HbA5XFmD-1651491444179)(https://bkimg.cdn.bcebos.com/formula/4ca01d1c773aa00a16428768aec6e080.svg)]](/img/a6/f4b5ab6a9ab6782688d5cd082e1f7a.png)
from Mathematical induction You can get :
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-kAiT2GFW-1651491444180)(https://bkimg.cdn.bcebos.com/formula/34b11f273583cfb988eb8e2e37354c4b.svg)]](/img/a6/f4b5ab6a9ab6782688d5cd082e1f7a.png)
, Substitute the general term of Fibonacci sequence into , Simplification results in .
Reference resources :
Baidu Encyclopedia
5、 ... and 、 Iterative implementation of Fibonacci number solution code demonstration
// The iterative method realizes the n Fibonacci number solution
#include<stdio.h>
int fib(int n)
{
if (n <= 2)
return 1;
int a = 1, b = 1,ret=0;
int i;
for (i = 3;i <= n;i++)
{
ret = a + b;
a = b;
b = ret;
}
return ret;
}
int main()
{
int n;
scanf("%d", &n);
int ret = fib(n);
printf("%d\n", ret);
return 0;
}
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-s3HC0QDR-1651491863542)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220502193520228.png)]](/img/e5/a7b1921d6bd1a96984dfa2f9403777.png)
边栏推荐
- [network] communication across regional networks learn how routing tables work
- 云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
- Implementation of strcat in C language
- Thoroughly understand bit operations - shift left, shift right
- Left alignment function of Lua language (handwriting)
- A chip company fell in round B
- BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
- Overcome the "fear of looking at teeth", and we use technology to change the industry
- MySQL8 Encrypting InnoDB Tablespaces
- 软考高级考试中有五大证书,哪个更值得考?
猜你喜欢

2022年下半年系统集成项目管理工程师认证8月20日开班

leetcode day1 分数排名

Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency

中国能否在元宇宙的未来发展中取得突破,占领高地?

The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced

Article translation software - batch free translation software supports major translation interfaces

Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich

Cloud computing notes part.1 - system management

Basic concept and essence of Architecture

How does app automated testing achieve H5 testing
随机推荐
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
时间转日期的sql语句应该怎么写?
Andorid system layout, values, drawable adaptation
Array method added in ES6
Deploy ZABBIX automatically with saltstack
Circular linked list OJ question
Idea properties file display \u solution of not displaying Chinese
【经验之谈】关于维修电子设备的几点建议和经验
CDGA|工业互联网行业怎么做好数据治理?
MySQL performance testing tool sysbench learning
Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers
MySQL 8 creates master-slave replication based on Clone
一文读懂如何部署具有外部数据库的高可用 K3s
The peak rate exceeds 2gbps! Qualcomm first passed 5g millimeter wave MIMO OTA test in China
克服“看牙恐惧”,我们用技术改变行业
Source insight project import and use tutorial
This customized keyboard turns me on~
远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
Implementation of memcpy in C language
冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题