<?php
header('content-type:text/html;charset=utf-8');
//菲波那切数列 1,1,2,3,5,8,13,,,
/**迭代方式实现菲波那切数列
* [feibo description]
* @param [type] $num [description]
* @return [type] [description]
*/
echo "迭代方式实现菲波那切数列";
echo "<br/>";
function feibo($num)
{
if ($num<1) {
return -1;
}
$arr[0]=1;
$arr[1]=1;
for ($i=2; $i <=$num ; $i++) {
$arr[$i]=$arr[$i-1]+$arr[$i-2];
}
return $arr;
}
echo implode(',', feibo(20));
echo "<hr/>";
/**递归方式实现菲波那切数列
* [diegui description]
* @return [type] [description]
*/
echo "递归方式实现菲波那切数列";
echo "<br/>";
function diegui($num)
{
if ($num<1) {
return -1;
}
if ($num==1||$num==2) {
return 1;
}
return diegui($num-1)+diegui($num-2);
}
$diegui ='';
for ($i=1; $i <=10 ; $i++)
{
$diegui.=diegui($i).',';
}
echo $diegui;
?>
header('content-type:text/html;charset=utf-8');
//菲波那切数列 1,1,2,3,5,8,13,,,
/**迭代方式实现菲波那切数列
* [feibo description]
* @param [type] $num [description]
* @return [type] [description]
*/
echo "迭代方式实现菲波那切数列";
echo "<br/>";
function feibo($num)
{
if ($num<1) {
return -1;
}
$arr[0]=1;
$arr[1]=1;
for ($i=2; $i <=$num ; $i++) {
$arr[$i]=$arr[$i-1]+$arr[$i-2];
}
return $arr;
}
echo implode(',', feibo(20));
echo "<hr/>";
/**递归方式实现菲波那切数列
* [diegui description]
* @return [type] [description]
*/
echo "递归方式实现菲波那切数列";
echo "<br/>";
function diegui($num)
{
if ($num<1) {
return -1;
}
if ($num==1||$num==2) {
return 1;
}
return diegui($num-1)+diegui($num-2);
}
$diegui ='';
for ($i=1; $i <=10 ; $i++)
{
$diegui.=diegui($i).',';
}
echo $diegui;
?>