PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: fibonacci number

## Pages

fibonacci number - phponwebsites.com
Showing posts with label fibonacci number. Show all posts

# Fibonacci series of given number in PHP

This blog describes about how to find Fibonacci series of given number in PHP. Fibonacci series is a sequence of number generated by adding previous two numbers. If the given number is 'n', then Factorial of 'n' is defined as 'Fn'. In mathematical terms, Fibonacci series for 'n' number looks like this:

Fn = Fn-1 + Fn-2;

Where,
F0 = 0;
F1 = 1;

Fibonacci series for 'n' number is calculated recursively by above formula.

### Simple PHP program Fibonacci series:

This is the simple PHP program for find Fibonacci series.

function fibonacci_series(\$n) {
\$f1 = -1;
\$f2 = 1;

for (\$i = 1; \$i <= \$n; \$i++) {
\$f = \$f1 + \$f2;
\$f1 = \$f2;
\$f2 = \$f;
echo "\$f<br />";
}
}

echo fibonacci_series(5);

The above PHP program returns 0, 1, 1, 2, 3. As i said, Fibonacci series is a sequence of number generated by adding previous two numbers. In above program, we have generated Fibonacci series for 5.
We have assumed \$f1 = -1; and \$f2 = 1; We need to run for loop until 5.As per above program,

If \$i = 1:
\$f = -1 + 1 = 0;
\$f1 = 1;
\$f2 = 0;

If \$i = 2:
\$f = 1 + 0 = 1;
\$f1 = 0;
\$f2 = 1;

If \$i = 3:
\$f = 0 + 1 = 1;
\$f1 = 1;
\$f2 = 1;

If \$i = 4:
\$f = 1 + 1 = 2;
\$f1 = 1;
\$f2 = 2;

If \$i = 5:
\$f = 1 + 2 = 3;
\$f1 = 2;
\$f2 = 3;

### Fibonacci series for 'n' number in PHP:

The below PHP program is used to find Fibonacci series of given number.

<html>
<head>
<style type="text/css">
body {
color:white;
font-size:14px;
}
.contact {
text-align:center;
background: none repeat scroll 0% 0% #8FBF73;
padding: 20px 10px;
box-shadow: 1px 2px 1px #8FBF73;
border-radius: 10px;
width:510px;
}
#number {
width: 250px;
margin-bottom: 15px;
background: none repeat scroll 0% 0% #AFCF9C;
border: 1px solid #91B57C;
height: 30px;
color: #808080;
border-radius: 8px;
box-shadow: 1px 2px 3px;
padding: 3px 4px;
}
#submit
{
background:none repeat scroll 0% 0% #8FCB73;
display: inline-block;
padding: 5px 10px;
line-height: 1.05em;
box-shadow: 1px 2px 3px #8FCB73;
border-radius: 8px;
border: 1px solid #8FCB73;
text-decoration: none;
opacity: 0.9;
cursor: pointer;
color:white;
}
#er {
color: #F00;
text-align: center;
margin: 10px 0px;
font-size: 17px;
}
</style>
</head>
<body>
<div class="contact">
<h1>Fibonacci series in PHP</h1>
<form action="#" method="POST">
Enter number : <input type="text" name="number" id="number" /></br>
<input  type="submit" name="submit" id="submit" value="Submit"></input>
</form>

<?php
if(isset(\$_POST['submit'])) {
\$n = \$_POST['number'];

function fibonacci_series(\$a) {
\$f1 = -1;
\$f2 = 1;

for (\$i = 1; \$i <= \$a; \$i++) {
\$f = \$f1 + \$f2;
\$f1 = \$f2;
\$f2 = \$f;
echo "\$f<br />";
}
}

echo "Fibonacci series of \$n is:<br />";
echo fibonacci_series(\$n);

}

?>
</div>
</body>
</html>

When you open above PHP file in browser, it looks like below image:

If you type number 6 in textbox, it returns 0, 1, 1, 2, 3, 5. It looks like below image:

Now you can find Fibonacci series for any number in PHP. I've hope, you got an idea for generate Fibonacci series of given number.