The beauty of programming 2.9 Fibonacci (FIBONACCI) sequence

Source: Internet
Author: User

The beauty of programming 2.9 Fibonacci (FIBONACCI) sequence

The recursive expression of Fibonacci is as follows

  • F (n) =f (n-1) +f (n-2) n>=2
  • F (1) =1
  • F (0) =0

The book mentions the three-way solution

    • The first one: the direct use of recursive method to solve
Package Org.wrh.programbeautiful;import Java.util.Scanner; Public classTopic2_9 { Public Static void Main(string[] args) {Topic2_9 t=NewTopic2_9 (); Scanner sc=NewScanner (System.inch);intN=sc.nextint ();LongTime1=system.currenttimemillis (); System. out. println ("Fibonacci in the series "+n+"number is:"+t.fibonacci (n)); System. out. println ("time required to calculate S"+ (System.currenttimemillis ()-time1)); } Public int Fibonacci(intN) {if(n<=0){return 0; }Else if(n==1){return 1; }Else returnFibonacci (n1) +fibonacci (n2); }}

The time complexity of this method is: O (2^n)

Since this method has a large number of repetitions in the calculation, we can make a record of the calculated F (n), and when we need to calculate this value, it is good to take it out directly, so we can reduce the time complexity, which is our common method of " space for Time ".

The implementation code is as follows:

Package Org.wrh.programbeautiful;import Java.util.arrays;import Java.util.Scanner; Public classTopic2_9 {int[] arr; Public Topic2_9(intN) {arr=New int[n+1]; } Public Static void Main(string[] args) {//topic2_9 t=new topic2_9 ();Scanner sc=NewScanner (System.inch);intN=sc.nextint (); Topic2_9 t=NewTopic2_9 (n);LongTime1=system.currenttimemillis (); System. out. println ("Fibonacci in the series "+n+"number is:"+t.fibonacci (n)); System. out. println ("time required to calculate s:"+ (System.currenttimemillis ()-time1)); System. out. println (Arrays.tostring (T.arr)); } Public int Fibonacci(intN) {if(arr[n]!=0){//If the data has already been calculated, return it directly            returnArr[n]; }Else{if(n<=0){return 0; }Else if(n==1) {arr[n]=1;return 1; }Else{arr[n]= Fibonacci (n1) +fibonacci (n2);returnArr[n]; }        }    }//public int Fibonacci (int n) {//if (n<=0) {//return 0;//      }/Else if (n==1) {//return 1;//          //      }/Else return Fibonacci (n-1) +fibonacci (n-2);//      //  }}

The code commented out above is code that does not introduce "space" to save the data.


    • The second kind: Find out the general expression of the solution (according to the knowledge of the signal system)
      Since the recursive expression of Fibonacci is: F (n) =f (n-1) +f (n-2), the characteristic equation is:
      X^2+x=1: x1= (1+SQRT (5))/2,x2= (1+SQRT (5))/2, so that its general solution can be obtained, and finally the parameters of the F (n) are calculated according to the initial value to obtain the expression of n, so we can be in the time complexity of O (1) Time to find out f (n)
      But when n is large, the time spent is still huge, so the following is a simple introduction to the N-th-squared approach to a number: two-point exponentiation
/* aThe idea of two-part exponentiation is as follows: Seeking A^n (n-th) if n is an even number (i.e. n%2==0) is equivalent to the original (a*a) ^ (n/2), if it is odd, ask for a^ (n1) * A (at this time N-1It becomes an even number). So the implementation code is as follows*/: Power_n (intAintN) {if(n==0)return 1;Else if(n==1)returnAElse if(n%2==0)returnPower_n (A*a, n>>1) ;Else        returnA*power_n(A, n1) ;} can also be used whileRealization: It can be understood that the N power series is expanded, and the cycle I is using the coefficient of the first Order power series. Long Pow1_n (intAintN) {long d=1, T=a; while(n>0)    {if(n%2==1) d= (d*t); n>>=1; T=t*t; }returnD;} Here's2The shift operation greatly accelerates the speed. Then for many times the power operation can use the binary power operation to calculate the complexity of LOGN.


    • The Third Kind: divide and conquer the strategy to solve (the book explanation is clearer)

is the use of [F (n), F (n-1)]=[f (n-1), F (n-2)]*a, where A is a 2*2 matrix,
Such an expression, and came to the
[F (n), F (n-1)]=[f (1), F (0)]* (a^ (n-1)), i.e. f (n) conversion in order to find the matrix A
Power, so the implementation code is similar to the above.
This involves the second-order matrix multiplication operation, the direct use of matrix multiplication is better than the definition of the implementation. If the multiplication of the higher-order matrix is involved, then the multiplication of the block matrix should be used to solve, in the introduction of the algorithm of the book is explained in detail, here no longer introduce

The beauty of programming 2.9 Fibonacci (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: 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.