[C language] Fibonacci sequence (Fibonacci sequence) recursive implementation and non-recursive implementation

Fibonacci sequence leads

The Fibonacci sequence is widely used and will not be listed here.

insert image description here

C language code implementation - recursion

At the same time, there are many kinds of code implementation methods, here are only the implementation methods in C language:

The core idea is to use "recursion".

//C语言:递归实现 Fibonacci数列
//输入n,求 Fibonacci[n]   (在数学中,常常写作Fibonacci(n) ) 
#include <stdio.h>
#define N 100
long Fibonacci(int n);
int main(){
    
    
	int n;
	long k;
	printf("请输入n:");
	scanf("%d",&n);
	k=Fibonacci(n);
	putchar('\n');
	printf("Fibonacci(%d)=%ld\n",n,k);
	
	
	return 0;
	
}
long Fibonacci(int n){
    
    
	if(n==1||n==2)
	{
    
    
		return 1;
	}	
	else{
    
    
		return Fibonacci(n-1)+Fibonacci(n-2);
	}		
}

A few tests:

insert image description here

insert image description here

In the following example, when the data is too large, an error should occur: due to precision issues, representation range issues.
Positive solution: Fibonacci(50) = 12 586 269 025
and pay attention to the time-consuming below, which is also very long.
insert image description here

Looking at the textbook, it can be seen that long intthe range of representation is:
-2 147 483 648 ~ 2 147 483 647(10 digits). And the result of Fibonacci(50) above is 11 digits, of course it will go wrong!

Also, the following result is incorrect:
also because of representation range issues.
Correct solution: Fibonacci(49) = 7 778 742 049
insert image description here
Let's test 正确the results below: ↓ ↓ ↓
In the local test, the 46th item, Fibonacci(46), should be the maximum value that long int can test.
It took 8.822 seconds.
insert image description here

C language code implementation - non-recursive

//非递归实现Fibonacci数列
#include <stdio.h>
long Fibonacci(int n);
int main(){
    
    
	int n;
	long k;
	printf("请输入n:");
	
	scanf("%d",&n);
	k=Fibonacci(n);
	
	putchar('\n');
	printf("Fibonacci(%d)=%ld\n",n,k);
	
	return 0;
}
//非递归实现Fibonacci数列 
long Fibonacci(int n)
{
    
    
	int n1=1,n2=1;
	int t=0,i=0;
	if (n<3)
	{
    
    	//即如果n为1或者2的话,那么Fibonacci(1) = Fibonacci(2)=1 
		return 1;
	}
	else  //下面举个例子,便于理解: 
	{
    
    //例如n为6,我们知道Fibonacci数列为:1、1、2、3、5、8...
	//此时对于下面for循环,则i=0,i<4。开始时n1和n2都是1,即第一和第二项 
		for(i=0; i<n-2; i++)
		{
    
    	
			t = n1 + n2; //t就是数列第三项,为 1+1=2 
			n1 = n2; //注意:令n2赋值给n1,即n1和n2分别 后移一位,这时新的n1即第二项F(2)=1 
			n2 = t;	//这里同理,t作为新的n2,即F(3)=2 
			//依次类推,即可实现求每一项,然后得到 Fibonacci(6),即 Fibonacci(n) 
		}//注意:如果还不好理解,那么动笔写一下就会理解啦! 
		return t;
	}
}

I think I have written the comments clearly. If you don't understand, take a look at the following picture, it should be well understood:
insert image description here

Then there are a few tests:
insert image description here
insert image description here
insert image description here
it turns out that the above results are correct.

(In addition, for the first 100 items of the Fibonacci sequence, I will upload it to " 资源" in a txt format, which can be downloaded directly.)

As for which one to choose, it is recommended for beginners to use the recursive method, which is easier to understand.

Guess you like

Origin blog.csdn.net/qq_44731019/article/details/123613913