当前位置:网站首页>[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)
边栏推荐
- The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
- Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)
- Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
- Edge detection and connection of image segmentation realized by MATLAB
- Information management system and games based on C language
- MySQL command statement (personal summary)
- 党员故事|李青艾用漫画带动农民增收致富
- Cloud computing notes part.2 - Application Management
- String中常用的API
- Machine learning -- model evaluation, selection and verification
猜你喜欢

String中常用的API

adb remount of the / superblock failed: Permission denied

Edge detection and connection of image segmentation realized by MATLAB

Getting started with enterprise distributed crawler framework

冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题

JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver

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

利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)

认识中小型局域网WLAN

What is the process of swing event processing?
随机推荐
C language pointer and two-dimensional array
leetcode day3 查找重复的电子邮箱
KPMG China: insights into information technology audit projects of securities fund management institutions
XOR operation and its usage
Leetcode day3 find duplicate email addresses
English translation Italian - batch English translation Italian tools free of charge
MySQL命令语句(个人总结)
leetcode day3 超过经理收入的员工
English translation Arabic - batch English translation Arabic tools free of charge
C language array and bubble sort
Source code analysis of scripy spider
MySQL performance testing tool sysbench learning
Basic usage of docker
党员故事|李青艾用漫画带动农民增收致富
String中常用的API
MATLAB实现的图像分割之边缘检测和连接
Labelme(一)
Find the memory occupied by the structure
Function fitting based on MATLAB
Special draft of Mir | common sense knowledge and reasoning: representation, acquisition and application (deadline on October 31)