Fibonacci numbers in Icon
Example for versions
iconc 9.4
This example uses recursive calculation of Fibonacci numbers with memoization.
global fib_memo
procedure fib (n)
if n >= 0 then
return ((/fib_memo [n] := fib (n - 2) + fib (n - 1)) | fib_memo [n])
end
procedure main ()
local i
fib_memo := table ()
fib_memo [0] := 0; fib_memo [1] := 1
every i := 1 to 16 do {
writes (fib (i) || ", ")
}
write("...")
end
Comments
]]>blog comments powered by Disqus
]]>