Anonymous

How To Make A Fibonacci Code Using While Loop?

1

1 Answers

Anonymous Profile
Anonymous answered
This is PHP, but you can easily convert it to anything else. The only use I can find for a while loop in this is to restrict how long the script should go on for, so I set it to stop under 1,000,000 is reached - obviously that could be set to anything.
<?
$last = 0;
$now = 1;
$next = 1;
echo "0, 1";

while ($next+$now < 1000000){
$now = $last;
$last = $next;
$next = $last+$now;
echo ", ".$next;
}
?>

Answer Question

Anonymous