Title:
Classical question: There are a pair of rabbits, from the 3rd month after birth a pair of rabbits each month, the small rabbit to the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask the total number of rabbits each month?
Fibonacci Number:
Also known as the Fibonacci sequence (Italian: Successione di Fibonacci), also known as the Golden Section, Faipot, Faipot, Sinorhizobium fredii series, refers to such a series: 0, 1, 1, 2, 3, 5, 8, 13, 、...... In mathematics, the Fibonacci sequence is defined recursively as follows: F0=0,f1=1,fn=fn-1+fn-2 (n>=2,n∈n*), in words, the Fibonacci sequence starts with 0 and 1, and then the Fibonacci sequence coefficients are added by the previous two numbers.
1 Packageprogramme;2 /**3 * 4 * Fipolaci sequence problem syntax definition: F (0) =0,f (1) =1,f (n) =f (n-1) +f ( n-2) (n≥2,n∈n*)6 * Use recursion method time:7 */8 Public classfibonaccirecursion {9 Public Static voidMain (string[] args) {Ten //Scanner scanner=new Scanner (system.in); One //int index=scanner.nextint (); A LongTime=System.currenttimemillis (); - intIndex=32; - for(inti = 0; I <index; i++) { theSystem.out.println ("the" + (i+1) + "Rabbit logarithm of the Month:" +Getfibonacci (i)); - } -System.out.println ("Recursive time-consuming:" + (System.currenttimemillis ()-Time )); - } + - //Recursive method + Public Static intGetfibonacci (intN) { A if(n==0| | N==1) at return1; - Else - returnGetfibonacci (n-1) +getfibonacci (n-2); - } - -}
1 Packageprogramme;2 /**3 * 5 * Fipolaci sequence problem syntax definition: F (0) =0,f (1) =1,f (n) =f (n-1) +f ( n-2) (n≥2,n∈n*)6 * Time spent using traditional methods: 17 */8 Public classfibonaccitradition {9 Public Static voidMain (string[] args) {Ten LongTime=System.currenttimemillis (); OneSystem.out.println ("The 1th month of the rabbit logarithm: 1"); ASystem.out.println ("The 2nd month of the rabbit logarithm: 1"); - intI=32; - intS1=1,s2=1,count=0; the for(intj = 3; j<=i; J + +) { -count=s1+S2; -s1=S2; -S2=count; +System.out.println ("+j+" month of the Rabbit logarithm: "+count); - } +System.out.println ("Traditional time-consuming:" + (System.currenttimemillis ()-Time )); A } at}
1 Packageprogramme;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 7 /**8 * Ten * Fipolaci sequence problem syntax definition: F (0) =0,f (1) =1,f (n) =f (n-1) +f ( n-2) (n≥2,n∈n*) One * Time spent using collections: 3 A */ - Public classfibonaccicollection { - PrivateList<integer> list=NewArraylist<integer>(); the - Public Static voidMain (string[] args) { - LongTime=System.currenttimemillis (); -Fibonaccicollection demo=Newfibonaccicollection (); + intIndex=24; - demo.put (index); + Demo.get (index); ASystem.out.println ("Collection Time:" + (System.currenttimemillis ()-Time )); at } - Private voidPutintN) { -List.add (1); -List.add (1); - for(inti = 3; I <= N; i++) { -List.add (List.get (i-2) +list.get (i-3)); in } - } to Private voidGetintN) { + intI=0; - for(intj:list) { theSystem.out.println ("the" + ++i + "months of the rabbit logarithm:" +j); * } $ }Panax Notoginseng - the +}
Fipolaci Series (Traditional rabbit problem)