Return to Snippet

Revision: 2218
at January 17, 2007 09:15 by koncept


Initial Code
<?php 
/**
 * Example Fibonacci implementation to (n) steps (without recursion)
 * @return array $req Array of Fibonacci numbers
 */
function Fibonacci( $steps = 20 )
{
	list( $cur, $nxt, $inc, $seq ) = array( 0, 1, 1, array() );
	
	do
	{
		$inc++;
		$seq[] = $cur;
		$add   = $cur + $nxt;
		$cur   = $nxt;
		$nxt   = $add;
	} while ( $inc <= $steps );
	
	return $seq;
}

// Example of first 50 Fibonacci numbers
print_r( Fibonacci( 50 ) );
?>

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

Initial Description


Initial Title
PHP Fibonacci Sequence

Initial Tags
php, function

Initial Language
PHP