Title: Write a function, enter n, and find the nth of the Fibonacci Sequence.
1 packagesolution;2 3 /**4 * Sword Point offer question 9: Fibonacci sequence5 * Title: Write a function, enter n, and find the nth of the Fibonacci Sequence. 6 * 0, n=17 * The Fibonacci sequence is defined as follows: f (n) = 1, n=28 * F (n-1) +f (n-2), n>29 * @authorGLTen * one */ a public classNo9fibonacci { - - public Static voidmain (string[] Args) { theSystem.out.println (the Value of the 4th Xiang Feipo sequence is: "+fibonacci (4)); - } - - /* + * Recursive implementation of Fibonacci sequence generation function with low efficiency - */ + public Static intGeneratefibonacci (intN) { a if(n==0) at return0; - if(n==1) - return1; - returnGeneratefibonacci (n-1) +generatefibonacci (n-2); - } - in /* - * Using loops to achieve Fibonacci sequences to * Store the middle of the sequence to obtain the result + */ - public Static intFibonacciintN) { the int[] result={0,1}; * if(n<2) $ returnresult[n];Panax Notoginseng intFibnminusone=1; - intFibnminustwo=0; the intFibn=0; + for(inti=2;i<=n;i++){ aFibn=fibnminusone+fibnminustwo; thefibnminustwo=fibnminusone; +Fibnminusone=fibn; - } $ returnfibn; $ } -}
Sword Point offer programming Java Implementation--face Test 9 Fibonacci sequence