Fibonacci Sequence in PHP

by Tyler Perroux on January 11, 2010

UPDATE: ADDED THREE NEW WAYS THAT YOU CAN DO THIS IN PHP.
IF YOU WANT TO SEE IT IN ACTION, CLICK HERE
Well I finally got it to work, and I felt that I should share this with the Open Source world.

Here it is, the Fibonacci Sequence coded in PHP.

If you are looking for some webhosting to practice with PHP, TylerPerroux.com recommends Bluehost.com

WARNING: THIS CAN CAUSE YOUR SERVER/COMPUTER TO CRASH IF USED INDIRECTLY. HOPEFULLY PHP WOULD CATCH THIS, AS THIS SCRIPT CAN CAUSE A NEAR ENDLESS LOOP. THERE IS NO WARRANTLY, EITHER IMPLIED OR EXPRESSED THAT THIS SCRIPT WORKS. ANY HARM THAT MAY HAPPEN BECAUSE OF THE IMPROPER/PROPER USE OF THIS SCRIPT IS IN NO WAY MY FAULT. FULL DISCLAIMER.

<?php
/**
* Example Fibonacci implementation to (n) steps (without recursion)
* @return array
@var $req - Array of Fibonacci numbers
*/
function fibonacci1( $steps = 10 )
{
list($current,$next,$curnum,$seq ) = array( 0, 1, 1, array() );

do
{
$curnum++;
$seq[] = $current;
$add   = $current + $next;
$current   = $next;
$next   = $add;
} while ( $curnum <= $steps );

return $seq;
}

// Example of first 15 Fibonacci numbers
echo "1) ";
print_r(fibonacci1(15));

echo "<br/>";
echo "2) ";

// This changes it from a multi-dimensional arry to a normal one.
foreach(fibonacci1(15) as $asdd => $asddd){
echo $asddd . " ";
}

function fibonacci2($n)
{
if($n == 0)
return 0;
elseif ($n == 1)
return 1;
else
return fibonacci2($n - 1) + fibonacci2($n - 2);
}
echo "<br>";
echo "3) ";

for($i = 0; $i < 15; $i++)
{
echo fibonacci2($i) . " ";
}
?>

<?php
/**
* The fibonacci function should look familiar.
*/
echo "<br>";

function fibonacci3($n){
$a = 0;
$b = 1;
for ($i = 0; $i < $n; $i++){
printf("%d\n",$a);
$sum = $a+$b;
$a = $b;
$b = $sum;
}
}echo "4) ";

fibonacci3(15);
echo "<br>";
show_source(__FILE__);
?>

Alright this explains everything.
1) This is a function that outputs a multi-dimensional array, without using recursion. $current_Number => $fibonacci_Num
2) Uses the same thing as 1, but cleans it up to only output the $fibonacci_Num
3) This uses recursion, and the base of it is a use of the explicit formula given.
4) This is just a simple for loop to output the numbers. Easy.

–Tyler Perroux

  • Share/Bookmark

{ 0 comments }

First Result on Google using 100% Organic SEO

by Tyler Perroux on December 15, 2009

Well of course this is nothing new. I bet you see those ad’s that show how you can be number one on Google, and you have to pay some unworldly price to get it.

Well this is your solution, using 100% Organic SEO, which is free, to promote your site or blog. Now organic means you don’t use pesticides, or things that just aren’t natural. Well that’s exatly what SEO is.

Now what is SEO, well it’s an acronym for Search Engine Optimization. The simplest way to describe it is how a human reads. People would rather see a site like mysite.com/products/free-seo, than something like mysite.com/?cat=2&id=28. People don’t like numbers and variables, they like plain and simple SEO.

Now if you don’t believe me on being the first result in Google, just use some of the links below, and you will see my site right up there as the first result.

godaddy domain subdomain with apacheKeyword 1
godaddy subdomain with apacheKeyword 2
total number of lines in a projectKeyword 3
home directory of Apache on godaddyKeyword 4
godaddy dns record subdomain apacheKeyword 5

Now most of these keywords have hundreds of millions of pages competing with me, but the reason why my pages are higher up than their’s, is because I have good content, and I market my post’s well.
Now this is pretty good, I am competing with industry leading website and corporations like GoDaddy, Wordpress.org, Ubuntu Forums, and Trap17.

If you would like to know how you can copy my same results, and be the first result in Google, just enter your name and email below. You will be receiving a nice email with some great SEO and marketing tips for you blog or website.

This is going to be fun =)
Tyler Perroux

  • Share/Bookmark

{ 1 comment }