"C Language" for Fibonacci (Fibonacci) sequence entries (recursive method, non-recursive method)

Source: Internet
Author: User

The Italian mathematician, Leon, discovered this sequence in 1202 when he studied the problem of rabbits producing cubs. Set up a pair of rabbits each month to give birth to a rabbit, each newborn rabbit one months after birth and then the Cubs, if the rabbit is not dead. Q: A pair of rabbits, how many pairs of rabbits can be bred in a year? In nature there are two types of rabbits: one is the rabbit that can reproduce, referred to as the Big Rabbit, the newborn rabbit can not reproduce, referred to as a small rabbit, rabbit one months into a big rabbit. It is the sum of the big rabbits and the little rabbits.


 Month    Part Ⅰⅱ Ⅲ Ⅳ ⅤⅥ Ⅶ ⅧⅨⅩ Ⅺ Ⅻ
Large Rabbit logarithm1 1  2  3  5 8  -  + the - the144
Small Rabbit logarithm0 1  1  2  3 5  8  - + the -  the by December There were big rabbits 144 pairs, bunny 89 pair, total rabbit 144+89=233 from the table above:

① monthly small rabbit logarithm = The logarithm of the big Rabbit last month.
② each month the logarithm of the large rabbit is equal to the logarithm of the large rabbit and the small rabbit.
Comprehensive ①② Two points, we have: the monthly large rabbit logarithm is equal to the first two months the sum of the large rabbit logarithm.


If the N-month large rabbit logarithm is expressed in un, there is
UN = un-1 + un-2, n >2 each month the number of large rabbits in the series is: 1,1,2,3,5,8,13,21,34,55,89,144, /c5> this sequence is called the Fibonacci sequence.


Recursive method:

Using the formula F[n]=f[n-1]+f[n-2], recursive calculation in turn, the recursive end condition is f[1]=1,f[2]=1.

code example:

#include <iostream>using namespace std;long long fib (int n) {     if  (n == 0)     {         return 0;    }    else if  (n == 1)      {        return 1;    }     else if (n > 1)     {         return fib (n - 1)  + fib (n - 2);     }    //return n > 1 ? fib (n - 1)  +  Fib (N&NBSP;-&NBSP;2)  : n; //conditional operator is simple, one line of code can be}void test () {    int n &NBSP;=&NBSP;0;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d",  &n);     int ret = &NBSP;FIB (N);     printf ("%d\n",  ret);} Int main () {    test ();     system ("Pause");     return 0;}

However, the recursive approach to solving this problem is not efficient, let's look at the non-recursive method.



Non-recursive method:

Iterative implementations are the most efficient, with a time complexity of n*1 = 0 (n) and a spatial complexity of 0 (1).

#include <iostream>using namespace std;long long fib (int n) {     if  (n == 0)     {         return 0;    }    else if  (n == 1)      {        return 1;    }     else if  (n > 1)     {         int a = 1;        int  b = 1;        int c = 1;         for  (int i = 2; i < n; i++)         {             c = a + b;            a = b;             b = c;         }        return c;    }}void  test () {    int n = 0;    scanf ("%d",  &N) ;     int ret = fib (N);     printf ("%d\n",  ret);} Int main () {    test ();     system ("Pause");     return 0;}


This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1755424

"C Language" for Fibonacci (Fibonacci) sequence entries (recursive method, non-recursive method)

Related Article

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.