Fibonacci numbers in bc
Example for versions
bc 1.06
This example uses recursive definition of Fibonacci numbers. Note that printing “…” is put into the loop, since both code and output are shown in the same console, and printing “…” after the loop will result in the following output:
for (n = 1; n <= 16; n++) {
print fibonacci(n); ", "
}
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, print "..."
...
define fibonacci(n) {
if (n <= 2) return(1);
return(fibonacci(n-1)+fibonacci(n-2));
}
for (n = 1; n <= 16; n++) {
print fibonacci(n); ", "
if (n==16) print "..."
}
Comments
]]>blog comments powered by Disqus
]]>