"C + +" Fibonacci sequence

Source: Internet
Author: User

The Fibonacci sequence (Fibonacci sequence), also known as the Golden Section, was introduced by the mathematician Leonardo's Fibonacci (Leonardoda Fibonacci) as an example of rabbit reproduction, so called the "rabbit sequence", Refers to a series of: 0, 1, 1, 2, 3, 5, 8, 13, 21, 、...... In mathematics, the Fibonacci sequence is defined recursively as follows:

F (0) =0, (n = 0)

F (1) =1, (n = 1)

F (n) =f (n-1) +f (n-2) (n≥2,n∈n*)


Fibonacci numbers refer to such a sequence of 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368

In particular, the No. 0 item is 0, and the 1th item is the first 1.

This sequence starts with the 2nd item and each item is equal to the sum of the first two items.

In real life, there are many examples of the Fibonacci sequence, and this is the classic question that is often in the interview.

So what does this seemingly bad-looking rule do with code?


Here are a few ways:

    1. Recursive implementations:
      < time complexity O (2^n) >

#include <iostream>using namespace Std;long long Fibonacci1 (long long N)//With a long long type considering the large number problem {if (N < 2) {   return n;   } else {return FIB (n-1) + fib (n-2);   }}int Main () {cout << Fibonacci1 (5) << Endl;//Find the value of an item system ("Pause"); return 0;}

Recursion seems to be straightforward, and it is less efficient if the number of items given is greater than N.


2. Non-recursive implementations:

< time complexity O (N) >

Long long Fibonacci2 (int n) {Long long * Fibarray = new long long[n+1];//array fibarray[0] = 0 based on number of items n; FIBARRAY[1] = 1; Manually set the first two numbers so that the next number of values can be calculated for (int i = 2; I <= n; ++i) {fibarray[i] = Fibarray[i-1] + fibarray[i-2];//Current number, etc.    In the first two digits plus} long long ret = fibarray[n];   Delete[] Fibarray; return ret;}

The non-recursive efficiency is higher than recursion, but the two meanings are the same.

Both of these methods need to be mastered.

The following two methods are integrated together:

#include  <iostream>using namespace std;long long fibonacci_1 (int n)//recursive {     if  (n<2)     {         return n;    }    return fibonacci_1 (n - 1)  + fibonacci_1 (N&NBSP;-&NBSP;2);} Void fibonacci_2 (INT&NBSP;N)//non-recursive {    int i;    long  LONG&NBSP;*FIBARRAY&NBSP;=&NBSP;NEW&NBSP;LONG&NBSP;LONG[N&NBSP;+&NBSP;1];&NBSP;&NBSP;&NBSP;&NBSP;FIBARRAY[0]  = 0;    fibArray[1] = 1;    for  (i =  2; i<n; i++)         fibArray[i] =  fibarray[i - 1] + fibarray[i - 2];    for  (i =  0; i<n; i++)         cout << fibarray[i] <<  " ";} Int main (void) {    int i, n, k;    printf (" Please enter the number of Fibonacci numbers  : ");     cin >>n;    printf (" Please select: 1. Recursion     2. Non-recursive  : ");    cin >> k;    if  (k  == 1)     for  (i = 0; i<n; i++)          cout << fibonacci_1 (i)  << " ";     else        fibonacci_2 (n);     system ("Pause ");     return 0;}

If there is a mistake, please correct me.


This article is from the "vs LV" blog, so be sure to keep this source http://survive.blog.51cto.com/10728490/1761712

"C + +" Fibonacci sequence

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.