Algorithm and data structure surface question (17)-Fibonacci number and __java

Source: Internet
Author: User
Tags first row
Topics


Title: The definition Fibonacci sequence is as follows:
/0 n=0
f (n) = 1 n=1
\ f (n-1) +f (n-2) n=2
8
Enter N, the quickest way to find the nth item of the sequence.
requires time complexity of Olog (n)


Thinking of solving problems


Previous articles: http://zhedahht.blog.163.com/blog/static/25411174200722991933440/


the problem translates to the following formula




This 2x2 matrix is represented by a matrix object. There are 4 properties, representing F (n), F (n-1), F (n-1), F (n-2), and F (n) to get the value of the first property.


the formula of the matrix is obtained by the method of division and treatment.




This gives the value of the n-1 of the matrix {1,1,1,0}.


The following is a formula that multiplies 2 matrices:




Code


public class Problem19 {    //2x2 matrix     class matrix2x2 {      
 //first Row first position         int pos00;
        //First row second position         int pos01;
        //second row first position         int Pos10;

        //second row second position         int pos11;         public matrix2x2 () {        }     & nbsp;   public matrix2x2 (int pos00, int pos01, int pos10, int pos11) {        &nb
sp;   this.pos00 = pos00;
            this.pos01 = pos01;
            this.pos10 = POS10;
            this.pos11 = POS11;         }     }     //2 2x2 matrix multiply to get new matrix      Class Matrixmultiply {        public matrix2x2 Multiply (matrix2x2 M1, matrix2x2 m2) {&nbsp ;           return New matrix2x2 (M1.POS00 * m2.pos00 + m1.pos01 * M2.POS10,                      m1.pos00 * m2.pos01 + m1.pos01 * M2.POS11, M1.pos10                     & nbsp;       * m2.pos00 + m1.pos11 * m2.pos10, M1.POS10         &NB sp;                   * m2.pos01 + m1.pos11 *
M2.POS11);         }     }     matrixmultiply multiplyer = new MATRIXMU

Ltiply ();     // Fibonacci number Nth number     public matrix2x2 getfibo (int n) {        if (n < 0) {            return null;         }  & nbsp
     matrix2x2 matrix22 = null;         if (n = = 1) {            matrix22 = NE
W matrix2x2 (1, 1, 1, 0);        &nbsp,}         //even        & Nbsp;else if (n% 2 = = 0) {            matrix22 = Getfibo (N/2);  &nbs P
         matrix22 = multiplyer.multiply (matrix22, matrix22);         }         //odd        & Nbsp;else if (n% 2! = 0) {            //extracts the first matrix, gives birthThe matrix is an even number, but keep in mind that the first matrix to be extracted is counted at the end.
            matrix22 = Getfibo ((n-1)/2);
            matrix22 = multiplyer.multiply (matrix22, matrix22);             matrix22 = multiplyer.multiply (matrix22, New matrix2x2 (1, 1, 1
, 0));
        }         return matrix22;     }     //original recursion     public static int GetFibo2 (int n) {   &nbsp ;    {            if (n==0| | n==1)             {          
      return n;            }             else             {                return GetFibo2 (n-1) +getfibo2 (n-2)
;            }         }      }          public static void Main (string[] args) {        in
T n = 30;
        matrix2x2 matrix = new Problem19 (). Getfibo (n-1);
        int fn = matrix.pos00;
        system.out.println (FN);
        int fn2 = Problem19.getfibo2 (n);
        system.out.println (FN2);
     }}


Output


832040
832040

Extended


Some netizens say can be solved by the following formula


Fibonacci General formula:

an=1/√[(1+√5/2) n (1-√5/2) n]


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.