fibonacci series in PHP

chris (2006-04-10 12:31:12)
1676 views
0 replies
This Fibonacci implementation is easy to convert into C or java or any language really. I've done it in PHP because it's quick and easy to read and understand. The secret to this is remembering to declare two inital values rather than just one so that you start of with a pair to sum. This is easily converted to java or C or whatever language you want to use.
<?php
   $first = 0;
   $second = 1;
   $n = 27;

   $inc = 1;

   do{
      $inc++;
      $final = $first + $second;
      $first = $second;
      $second = $final;
   }
   while ($inc < $n) ;

   print "nn$finalnn";
?>
Digg it! Submit to Slashdot Add to Blinklist Del.icio.us Add to Newsvine Add to Technorati Add it to Google Bookmarks Add to Magnolia
reply