Introduction
The Fibonacci sequence, also known as the Golden series, refers to such a series: 1, 1, 2, 3, 5, 8, 13, 21 ,...... In mathematics, the Fibonacci series are defined as follows by recursive Methods: f0 = 0, F1 = 1, FN = f (n-1) + f (n-2) (N> = 2, n, N *). I used recursive and iterative methods to implement the Fibonacci series.
Implementation Code (PHP)
<? Phpclass Fibonacci {/*** Description: gets the nth value of the Fibonacci using the iteration method ** @ Param int $ N * @ return int */public static function fib_interation ($ N) {$ fib = array (); // defines the Fibonacci array if ($ n <0) {return 0;} For ($ fib [0] = 0, $ fib [1] = 1, $ I = 2; $ I <= $ N; $ I ++) {$ fib [$ I] = $ fib [$ I-1] + $ fib [$ I-2];} return $ fib [$ N];} /*** Description: Obtain the nth value of Fibonacci using a recursive method. ** @ Param int $ N * @ return int */Public sta TIC function maid ($ n) {if ($ n <= 0) {return 0;} elseif ($ n = 1) {return 1;} else {return self :: fig + self: FIG ($ n-1. "\ n"; $ maid = MAID: Maid (10); echo $ maid. "\ n";?>
Running result
Disadvantages: today, I can see that this blog is still very popular, so I reconstructed the code. The level of the Code half a year ago is indeed poor, and the code looks disgusting. I Want To refactor it today! Secondly, the beauty of programming introduces a sub-governance strategy to find the Fibonacci method, but I am not familiar with it. If you are interested, you can post the implementation code to discuss it. C or PHP is recommended!