Recently, when I looked at the data structure and algorithm, I saw a poor solution to the simple problem. I gave a PHP version of the Fibonacci series solution by referring to the network information.
| 1234567891011121314151617181920212223242526272829303132 |
/** * Description: Fibonacci sequence (subscript starts from 0) * Author: junin927@hotmail.com, Junin South-North * Author Page: http://www.j927.net * Publish: 2011-01-02 * Last Modify: 2011-01-02 * Remark. * * @param int $n * @ Return int-1: input error n (n> 0): sequence result */function
fibonacci($n) {
// Iterative Solution if
($n<0)
return -1; if
($n<=1)
return 1; $f0
= $f1 = 1; for
($i=1;$i<$n;$i++) { $f
= $f0+$f1; $f0
= $f1; $f1
= $f; } return
$f;}/* Function fibonacci ($ n) {// recursive Solution if ($n<0) return -1; if ($n<=1) return 1; return fibonacci($n-1)+fibonacci($n-2);}*//* Function fibonacci ($ n) {// use the generic formula if ($n<0) return -1; $n++; return (pow((1 + sqrt(5))/2,$n)-pow((1-sqrt(5))/2,$n))/sqrt(5);}*/ |
Data Structure & algorithm php,
Data structure,
Fibonacci, Algorithm