[Job interview] Fibonacci series (C)

Source: Internet
Author: User

Classic is classic, no matter how many years, classic will never change.

The language and framework will be outdated one day, but the classic will always exist. This is to study these classicAlgorithmOf eternity.

When a. netProgramWhen I applied for a job in Java, C ++, and Android, I found that framework language features were all abandoned. In this way, the sponge is crowded and my water is coming out in four years. There are not many remaining vertices. Those who can run through each position are those who are around, but ignore the air-like, underlying knowledge and application capabilities.

How much moisture does your experience have? After a long time in a company, you will find that there are not many of you.

 

Interesting questions:

1There is a stairway10Level,It is specified that each step can only Span one or two levels,To attach10Several steps are different?

A:This is a Fibonacci series: there is a method of boarding the first level; there is a method of boarding the second level; there is a method of boarding the third level, there are two methods of boarding; climb the fourth level, there are three methods ...... So, 1, 1, 2, 3, 5, 8, 13 ...... Grade 10, 89 types.

2, The limit of the two adjacent items in the series compared to that of the next item, that is, whenNWhen it tends to be infinite,F (N)/F (n + 1)What is the limit?

A:This can be directly obtained by its general formula. The limit is (-1 + √ 5)/2. This is the so-called golden split point and a number representing the harmony of nature.

 

Mathematical representation:

The mathematical expression of the Fibonacci series is:

F (n) = f (n-1) + f (n-2)

F (1) = 1

F (2) = 1

 

The formula is provided, but why? Have you ever thought about it?

How is the formula extracted? I am not good at mathematics, so my experience begins with finding patterns:

If the starting point is 0, F (1) = 1 is recommended for the first level, and F (2) = 1 is recommended for the second level. For the third pole, there are two steps: 1 + 2, 2 + 1. For level 4 steps, you can + 1 from Level 3 or Level 2, that is, F (4) = f (3) + F (2 ), and so on.

WriteCodeIt is also very simple:

         ///  <Summary>  
/// The Recursive Algorithm of Fibonacci. Time complexity O (n) = O (3/2) ^ N), exponential Algorithms
/// </Summary>
Public Ulong Fibonaccirecursion ( Int N)
{
If (N < 0 )
Throw New Argumentoutofrangeexception ( " N must> 0. " );

If (N = 1 | N = 2 )
Return 1 ;
Return Fibonaccirecursion (n- 1 ) + Maid (n- 2 );
}

What is the efficiency of this super simple recursive program? Let's add an array to record the number of recursion times each time. At the same time, we can output these interesting numbers.

         ///  <Summary>  
/// Recursive output
/// </Summary>
Public Ulong Fibonaccirecursioncount ( Int N, Int [] Countarray)
{
Countarray [N] ++; // Count the compute number.

If (N < 0 )
Throw New Argumentoutofrangeexception ( " N must> 0. " );

If (N = 1 | N = 2 )
Return 1 ;
Return Fibonaccirecursioncount (n- 1 , Countarray) + fig (n- 2 , Countarray );
}

In this case, we can obtain the number of times each fib (I) is calculated:

FIB (10) = 1 fib (9) = 1 fib (8) = 2 fib (7) = 3

FIB (6) = 5 fib (5) = 8 fib (4) = 13 fib (3) = 21

FIB (2) = 34 fib (1) = 55 fib (0) = 34

It can be seen that the number of computations is in the back-to-back Fibonacci series, which clearly results in a large number of repeated computations.

We set T (n) to the running time of function fib (N). When n> = 2, we can see that:

T (n) = T (N-1) + T (N-2) + 2

While fib (n) = fib (n-1) + fib (n-2), so t (n)> = fib (n), the induction proof can be:

FIB (n) <(5/3) ^ n

WhenN> 4,FIB(N)> = (3/2) ^ n

Standard writing:

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.