Interview question 10: Fibonacci Series

Source: Internet
Author: User

The Fibonacci number refers to a series of values: 0, 1, 1, 2, 3, 5, 8, 13, 21 ,......
This series starts from the third number, and each subsequent number is obtained by the sum of the first two numbers.

We know that in programming we can use recursive and iterative methods to find the specified Fibonacci number, but these two methods have their own advantages and disadvantages.

Difference: the code written by recursive method (time complexity O (2 ^ n) is highly readable, which is equivalent to translating the mathematical formula in the book into code, but this method is too slow, when you calculate 50th Fibonacci numbers, your computer may have to compute for more than 10 minutes. In addition, recursion can easily cause stack overflow. Every time you call the evaluate function, you have to open up a space. The number of times that the 50th Fibonacci numbers call the function is terrible, similarly, the size of the space opened on the stack can be imagined, and stack overflow is normal.

Iteration Method: (time complexity O (N) is much more efficient than iteration method. iteration is obtained through loops. as long as three temporary variables are created, the Fibonacci number can be quickly obtained, and the speed is fast (for fear, the 100th Fibonacci number is just an instant), but the code of the iterative method is less readable, it is not easy for beginners to write such code.
---------------------
Tianzez
Source: csdn
Original: 78443321
Copyright Disclaimer: This article is an original article by the blogger. For more information, see the blog post link!

The following code is used:

  • Recursion
int Fab(int n){          if(n == 0)         return 0;      if(n == 1)         return 1;      return Fab(n-1) + Fab(n-2);}


  • Iteration Method
long long Fibnacci(unsigned n){        int result[2] = {0,1};    if(n < 2)        return result[n];    long long fibNMinusOne = 0;    long long fibNMinusTwo = 1;    long long fibN = 0;    for(int i = 2;i <= n;i++){        fibN = fibNMinusOne + fibNMinusTwo;        fibNMinusOne = fibNMinusTwo;        fibNMinusTwo = fibN;    }}

Interview question 10: Fibonacci Series

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.