Fibonacci numbers in Lua
Example for versions
Lua 5.0.3
Numbers which have already been calculated are stored in associative array fib
and are retrieved from it to calculate the next ones. By default Lua associative arrays use 1-based integer keys, so fib = {1, 1}
creates an array with indices 1 and 2.
fib = {1, 1}
for n = 3, 16 do
fib[n] = fib[n-1] + fib[n-2]
end
for n = 1, 16 do
io.write(fib[n], ", ")
end
io.write("...\n")
Comments
]]>blog comments powered by Disqus
]]>