Fibonacci numbers in bc
Example for versions
bc 1.06
This example uses Binet’s formula. Note that bc is an arbitrary-precision calculator, so after floating-point calculations the numbers have to be rounded to integers. This has to be done manually, since bc doesn’t provide built-in rounding.
for (n = 1; n <= 16; n++) {
scale = 10
x = (((1 + sqrt(5)) * .5) ^ n - ((1 - sqrt(5)) * .5) ^ n) / sqrt(5)
scale = 0
print (x+0.5)/1; ", "
if (n==16) print "..."
}
Comments
]]>blog comments powered by Disqus
]]>