Tutorials

Print Fibonacci Series in PHP [Code]

Fibonacci Series is a series of numbers in which each number (Fibonacci number) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 etc.

As seems that, the first two numbers in this sequence are either 1 and 1 or 0 and 1 depending on the chosen starting point of the sequence and each subsequent number is the sum of the previous two.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation:

    Fn = Fn-1 + Fn-2

with seed values

   F0 = 0 and F1 = 1.

If you want to print the fibonacci number for 10th then it will be 55.

Code for PHP:

<?php
function fib($n)
{
if ($n <= 1)
return $n;
return fib($n – 1) +
fib($n – 2);
}
$n = 10;
echo fib($n);
?>

Output: 55

In case, if you want to print the whole Fibonacci series in PHP then the code is:

<?php
$numbers=array(1, 1);
for($i=1;$numbers[$i]<=2000;$i++)
{
$numbers[]=$numbers[$i]+$numbers[$i-1];
}
echo “<p><pre>”;
print_r($numbers);
echo “</pre></p>”;
for($j=0;isset($numbers[$j]);$j++)
{
echo “<p>ID: “.$j.”<br />Value: “.$numbers[$j].”</p>”;
}
?>

Have something to say about this article? Comment below or share it with us on Facebook or Twitter.

H4ck0
Step by step hacking tutorials about wireless cracking, kali linux, metasploit, ethical hacking, seo tips and tricks, malware analysis and scanning.
https://www.yeahhub.com/

WHERE SHOULD WE SEND

HACKING TUTORIALS & INFOSEC NEWS?

Subscribe to Our Newsletter and Get Instant Delivered to Your Email Inbox.

We respect your privacy and take protecting it seriously.

Leave a Reply

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

2 × 3 =