Data Structure_Recursive Algorithm_Fibonacci Sequence

       Fibonacci sequence is a very important concept , which is given in Baidu Encyclopedia " Fibonacci sequence (Fibonacci sequence), also known as the golden section sequence, due to mathematician Leonardo Fibonacci Leonardoda Fibonacci was introduced with the example of rabbit breeding, so it is also called " rabbit sequence ", which refers to such a sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... Mathematically, the Fibonacci sequence is defined recursively as follows : F(1)=1, F(2)=1, F(n)=F(n-1)+F(n-2) (n>2, n∈N*) In the fields of modern physics, quasi-crystal structure, and chemistry, the Fibonacci sequence has direct applications. A mathematical journal under the name of Number Sequence Quarterly , dedicated to publishing research results in this area.

【General term formula calculation】

#include 
   
   
    
    
    
    
using namespace std;
    

    
    
double Fibon (int n)
    
    
{
    
    
    if (n < 1)
    
    
    {
    
    
        cout<<"n is error!"<
    
    
    
     
     
     >n;
     
     
    cost<
     
     
     
    
    
   
   

【Calculation by cycle method】

#include 
   
   
    
    
    
    
using namespace std;
    

    
    
int Fibon (int n)
    
    
{
    
    
    if (n < 0)
    
    
    {
    
    
        printf("n is error!\n");
    
    
        return -1;
    
    
    }
    
    
    int n1 = 1;
    
    
    int n2 = 2;
    
    
    int n3 = 3;
    
    
    for (int i = 3;i <= n;++i)
    
    
    {
    
    
        n3 = n1+n2;
    
    
        n1 = n2;
    
    
        n2 = n3;
    
    
    }
    
    
    return n3;
    
    
}
    

    
    
intmain()
    
    
{
    
    
    int n;
    
    
    cout<<"Please input Fibon index:"<
    
    
    
     
     
     >n;
     
     
    cout<<"Fibonacci Number 1"<
     
     
     
    
    
   
   

【Calculate recursively】

#include 
   
   
    
    
    
    
using namespace std;
    

    
    
int Fibon (int n)
    
    
{
    
    
    if (n < 0)
    
    
    {
    
    
        printf("n is error!\n");
    
    
        return -1;
    
    
    }
    
    
    int n1 = 1;
    
    
    int n2 = 2;
    
    
    int n3 = 3;
    
    
    for (int i = 3;i <= n;++i)
    
    
    {
    
    
        n3 = n1+n2;
    
    
        n1 = n2;
    
    
        n2 = n3;
    
    
    }
    
    
    return n3;
    
    
}
    

    
    
intmain()
    
    
{
    
    
    int n;
    
    
    cout<<"Please input Fibon index:"<
    
    
    
     
     
     >n;
     
     
    cout<<"Fibonacci Number 1"<
     
     
     
    
    
   
   

    Interestingly, such a sequence of completely natural numbers, the general term formula is expressed by irrational numbers . And when n tends to infinity, the ratio of the former item to the latter item is getting closer and closer to the golden ratio of 0.618 (or the fractional part of the ratio of the latter item to the former item is getting closer and closer to 0.618).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325713391&siteId=291194637