RosettaCodeData/Task/Fibonacci-sequence/Kabap/fibonacci-sequence.kabap

36 lines
738 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
// 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;
2026-04-30 12:34:36 -04:00
$i = $i++;
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
// Do the calculation for this loop iteration
$b = $a + $b;
$a = $b - $a;
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
// Add the result to the sequence
$sequence = $sequence << $a;
2023-07-01 11:58:00 -04:00
2026-04-30 12:34:36 -04:00
// Make the loop run a fixed number of times
if $i < $n; {
$sequence = $sequence << ", ";
goto calcnextnumber;
}
2023-07-01 11:58:00 -04:00
// Use the loop counter as the placeholder
$i--;
// Return the sequence
return = "Fibonacci number " << $i << " is " << $a << " (" << $sequence << ")";