https://www.cnsecer.com/4146.html
http://www.jb51.net/article/71424.htm
//一列数字的规则如下;1,1,2,3,5,8,13,21,34........ 求第30位数字是多少function Fibonacci($n) { $result = 1;//当n<=2时都返回1 if($n>2) //当n>2时,进行递归计算 { $result= Fibonacci($n-1)+Fibonacci($n-2); } return $result; }echo Fibonacci(3);
//使用非递归函数function Fibonacci_1($n){ $result = 1;//当$n<=2时都是返回1; if($n>2){ $first = 1; $second = 1; $i =0; $n = $n-2;//每次减少2次循环 while($i<$n){ $first = $second; $second = $result; $result = $first + $second; $i++; } } return $result;}echo Fibonacci_1(30);
时间: 2024-10-13 12:47:43