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

php实现斐波那契数列的简单写法_php技巧

WBOY
Release: 2016-05-16 20:39:48
Original
1056 people have browsed it

斐波那契数列是非常常见的一类数列,其数学定义为: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;>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template