The content of this article is about the implementation of Fibonacci sequence in PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it
一:function one($n){ $array = array(); $array[0] = 1; $array[1] = 1; for($i=2;$i<$n;$i++){ $array[$i] = $array[$i-1]+$array[$i-2]; } print_r($array); } one(10); echo "\n------------------\n"; 二:function two($n){ if($n==1||$n==2){return 1;} else{ return two($n-1)+two($n-2); } } echo two(10);die;
Related recommendations:
PHP implements the recursive algorithm and the iterative algorithm of the Tower of Hanoi problem
The above is the detailed content of How to implement Fibonacci sequence in php. For more information, please follow other related articles on the PHP Chinese website!