1) Array
(
[0] => 0
[1] => 1
[2] => 1
[3] => 2
[4] => 3
[5] => 5
[6] => 8
[7] => 13
[8] => 21
[9] => 34
[10] => 55
[11] => 89
[12] => 144
[13] => 233
[14] => 377
)
2) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
3) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
4) 0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
<?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.
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.