36 lines
712 B
Plaintext
36 lines
712 B
Plaintext
// Calculate the $n'th Fibonacci number
|
|
|
|
// Set this to how many in the sequence to generate
|
|
$n = 10;
|
|
|
|
// These are what hold the current calculation
|
|
$a = 0;
|
|
$b = 1;
|
|
|
|
// This holds the complete sequence that is generated
|
|
$sequence = "";
|
|
|
|
// Prepare a loop
|
|
$i = 0;
|
|
:calcnextnumber;
|
|
$i = $i++;
|
|
|
|
// Do the calculation for this loop iteration
|
|
$b = $a + $b;
|
|
$a = $b - $a;
|
|
|
|
// Add the result to the sequence
|
|
$sequence = $sequence << $a;
|
|
|
|
// Make the loop run a fixed number of times
|
|
if $i < $n; {
|
|
$sequence = $sequence << ", ";
|
|
goto calcnextnumber;
|
|
}
|
|
|
|
// Use the loop counter as the placeholder
|
|
$i--;
|
|
|
|
// Return the sequence
|
|
return = "Fibonacci number " << $i << " is " << $a << " (" << $sequence << ")";
|