Fibonacci numbers in Nimrod
Example for versions
Nimrod 0.8.8
This example uses Binet’s formula to calculate the numbers. Addition of epsilon value before rounding is necessary to get exact integer values.
from math import sqrt, pow, round
proc fibonacci(n: int): int =
var phi: float64 = (1.0 + sqrt(5.0)) / 2.0
return round((pow(phi, float64(n)) - pow(-phi, -float64(n))) / sqrt(5.0) + 0.0001)
var res = ""
for i in 1..16:
res = res & $fibonacci(i) & ", "
echo res & "..."
Comments
]]>blog comments powered by Disqus
]]>