Fibonacci sequence (recursive and non-recursive)


1. Definition

The Fibonacci sequence (Fibonacci sequence), also known as the golden section sequence, was introduced because of the mathematician Leonardoda Fibonacci (Leonardoda Fibonacci) taking rabbit reproduction as an example, so it is also called the "Rabbit Sequence", referring to Is a sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34,... In mathematics, the Fibonacci sequence is defined in a recursive manner as follows: F(0)= 0, F(1)=1, F(n)=F(n-1)+F(n-2) (n ≥ 3, n ∈ N*) In modern physics, quasi-crystal structure, chemistry and other fields, Fiji The Bonacci sequence has direct applications. For this reason, the American Mathematical Society has published a mathematics magazine named "Fibonacci Sequence Quarterly" since 1963 to publish research results in this area.

Second, recursively realize the Fibonacci sequence

Recursive implementation code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int fib(int n){
    
    
	if (n == 1){
    
    
		return 1;
	}
	if (n == 2){
    
    
		return 1;
	}
	return fib(n - 1) + fib(n - 2);
}

int main()
{
    
    
	int n = 0;
	printf("请输入要求第几个数字:");
	scanf("%d", &n);
	printf("%d\n", fib(n));
	system("pause");
	return 0;
}

result:

Enter 5 and output 5.

Insert picture description here

Enter 40 and output 102334155.

Insert picture description here

Enter 50 and output -298632863.
The reason for outputting a negative number here is that fib(50) is a large number, and int cannot be represented at all.

Insert picture description here

Analysis:
If we try to run the code ourselves, we can know
that the calculation speed is still very fast when inputting 5, when inputting 40, the calculation only takes a few seconds, and inputting 50, the calculation takes 6-7 minutes (and computer cpu related).
So let's think about whether there are any problems with using recursion to implement Fibonacci numbers?


Let's take input 5 as an example.

Insert picture description hereIt can be seen that fib(3), fib(2) and fib(1) have been repeatedly calculated many times. When entering a number of 40, 50... or greater, the number of repeated calculations will become more, so we consider using other methods to implement the Fibonacci sequence.

Third, realize the Fibonacci sequence in a loop

Loop implementation code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int fib(int n){
    
    
	if (n == 1){
    
    
		return 1;
	}
	if (n == 2){
    
    
		return 1;
	}
	int last1 = 1;
	int last2 = 1;
	int cur = 0;
	for (int i = 3; i <= n; i++){
    
    
		cur = last1 + last2;
		last2 = last1;
		last1 = cur;
	}
	return cur;
}

int main()
{
    
    
	int n = 0;
	printf("请输入要求第几个数字:");
	scanf("%d", &n);
	printf("%d\n", fib(n));
	system("pause");
	return 0;
}

result:

Enter 5 and output 5.

Insert picture description here

Enter 40 and output 102334155.

Insert picture description here

Enter 50 and output -298632863. The reason for outputting a negative number here is also that fib(50) is a very large number, and int cannot be represented at all.

Insert picture description here
Analysis:
When we use a loop to implement the Fibonacci sequence, the code runs very fast and only takes a few seconds.

Fourth, the experience of learning recursive algorithm

When I was learning about function recursion, I learned that recursion has one element: extracting repetitive logic and reducing the scale of the problem. I subconsciously think that the size of the problem is reduced and the code will run faster, but in fact it is not the case. The recursive algorithm requires more function calls. If the call layer is deeper, additional stack processing is required. For example, parameter transfer requires operations such as stacking, which will have a certain impact on execution efficiency.

Guess you like

Origin blog.csdn.net/qq_34270874/article/details/109334558