Home >Backend Development >PHP Tutorial >php实现斐波那契数列的简单写法_PHP

php实现斐波那契数列的简单写法_PHP

WBOY
WBOYOriginal
2016-06-01 11:49:351102browse

斐波那契数列是非常常见的一类数列,其数学定义为:F0=1,F1=1,Fn=F(n-1)+F(n-2)(n>=2,n∈N*)。本文就用php来简单实现斐波那契数列,代码十分简洁易懂,如下所示:

<&#63;php 
$arr[1] = 1;
for($i = 2;$i < 100;$i++)
{
    $arr[$i] = $arr[$i-1] + $arr[$i-2];
}
echo join(",",$arr);//将数组合并为一个字符串输出
&#63;>

至此就实现了Fn=F(n-1)+F(n-2)中n在100以内的斐波那契数列的显示输出。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn