<?php
//$all为1则返回fabnacci数列所有数组元素
function fabnacci($n, $all = 1){
static $fabn = [];
if($n < 2){
$fabn[$n] = 1;
}else{
if(empty($fabn[$n])){
$fabn[$n] = fabnacci($n - 1, 0) + fabnacci($n - 2, 0);
}
}
if($all == 1){
return $fabn;
}else{
return $fabn[$n];
}
}
var_dump(implode(" ", fabnacci(10)));
var_dump(implode(" ", fabnacci(10)));
?>
返回显示
string(26) "1 1 2 3 5 8 13 21 34 55 89" string(26) "1 1 2 3 5 8 13 21 34 55 89"