«

»

Jun 07

Fibonacci Series in PHP

A Fibonacci Series of ┬áNumber (0,1,1,2,3,5,8,13,21,34,55……)

A Fibonacci Series represent in Mathematical term.

Fn=Fn-1+Fn-2;

F0=0, F1=1

First two number of Fibonacci Series are 0 and 1. After that each number is sum of previous two number. Fibonacci Series was used in ancient india for metric science. It refered as ‘Golden Mean’ in roman.

Some method given below for calculate the Fibonacci Series in php.

Method 1

$count=0;
$a=0
$b=1
echo $a;
echo $b;
while($count<50)
{
$c=$a+$b;
echo $c;
$a=$b
$b=$c;
$count++;
}

Method 2

function fibonacci($n,$first = 0,$second = 1)
{
$fib = [$first,$second];
for($i=1;$i<$n;$i++)
{
$fib[] = $fib[$i]+$fib[$i-1];
}
return $fib;
}

print_r(fibonacci(50));

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>