PHP Fibonacci Sequence


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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. ?>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.