Posted By

koncept on 01/17/07


Tagged

php function fibonacci


Versions (?)

PHP Fibonacci Sequence


 / Published in: PHP
 

URL: http://en.wikipedia.org/wiki/Fibonacci_number

  1. <?php
  2. /**
  3.  * Example Fibonacci implementation to (n) steps (without recursion)
  4.  * @return array $req Array of Fibonacci numbers
  5.  */
  6. function Fibonacci( $steps = 20 )
  7. {
  8. list( $cur, $nxt, $inc, $seq ) = array( 0, 1, 1, array() );
  9.  
  10. do
  11. {
  12. $inc++;
  13. $seq[] = $cur;
  14. $add = $cur + $nxt;
  15. $cur = $nxt;
  16. $nxt = $add;
  17. } while ( $inc <= $steps );
  18.  
  19. return $seq;
  20. }
  21.  
  22. // Example of first 50 Fibonacci numbers
  23. print_r( Fibonacci( 50 ) );
  24. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: breck on November 14, 2007

Nice. Much faster than my implementation(using recursion--just for fun).

You need to login to post a comment.