Sword Point 9 (Java edition) Fibonacci sequence

Source: Internet
Author: User

Title One: Write a function, enter N, and find the nth term of the Fibonacci sequence. The Fibonacci sequence is defined as follows:


1, efficiency is inefficient solution, the picky interviewer will not like

Many C language textbooks in the time of the recursive function, the family take Fibonacci as an example, so many candidates for the problem of the recursive solution are very familiar.

Here is the implementation code


We have repeatedly used this problem in our textbooks to explain recursive functions, and it does not explain that recursive solutions are best suited to this topic. The interviewer will remind us that the above recursive solution has a very serious efficiency problem that requires us to analyze the cause.

We use the solution F (10) as an example to analyze the recursive solution process. To obtain F (10), F (9) and F (8) need to be obtained first. f (8) and F (7) are also required for the same (9). We use trees to construct this dependency. :


It is not difficult to find that there are many nodes in this tree that are duplicated, and that the number of repetitions increases sharply with the increase of N, which means that the computational amount increases sharply with the increase of N. In fact, the time complexity calculated by recursive method is incremented in the exponential way of N. Readers may wish to ask Fibonacci's 100th try and feel how slow the recursion will be.

2, the interviewer expected to apply the solution:

In fact, the method of improvement is not complicated. The recursive code mentioned above is slow because there are too many repetitive computations, so we just want to avoid repeating the calculation type. For example, we can save the already obtained sequence of the intermediate items, if we need to calculate the next time we look first, if the previous calculation has not been repeated calculation.

The simpler method is to calculate from the bottom up, first calculate f (0) and F (1) to figure out F (2), and then calculate F (2) According to F (1) and F (3) ... The nth item can be calculated by analogy. It is easy to understand that the time complexity of this idea is O (n). The implementation code is as follows:

/** * Write a function, enter N, and find the nth of the Fibonacci (Fibonacci) sequence. */package swordforoffer;/** * @author Jinshuangqi * * July 29, 2015 */public class E09fibonacci {public long Fibonacci (int n) {Long result =0;long PreOne = 1;long pretwo = 0;if (n = = 0) {return pretwo;} if (n = = 1) {return preOne;} for (int i = 2;i<= n; i++) {result = Preone+pretwo;pretwo = Preone;preone = result;} return result;} public static void Main (string[] args) {E09fibonacci Fabonacci = new E09fibonacci (); System.out.println (Fabonacci.fibonacci (10));}}
3, Time complexity O (LOGN) but not enough to use the solution)

Usually the interview is almost here, although we have a faster O (LOGN) solution, because this algorithm requires a very rare mathematical formula, so very few interviewers will ask us to master. But just in case, let's introduce this algorithm.

Let's talk first. A mathematical announcement of the sum:



Solution Comparison:

The time efficiency of the Fibonacci sequence is very different. The first recursive solution, though intuitive but inefficient in time, does not use this approach in real-world software development, and is unlikely to be favoured by interviewers. The second method uses the recursive algorithm to realize the loop, which greatly improves the time efficiency. The third method is a very creative algorithm to convert Fibonacci sequences into the exponentiation of matrices. Although we can Yo O (logn) to seek the matrix of the N-square, but because of the hidden time Changshu is larger, there will be few software to use this algorithm, in addition, the implementation of this algorithm code is also complex, not very suitable for interview.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sword Point 9 (Java edition) Fibonacci sequence

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.