Fibonacci numbers in Erlang
Example for versions
erl 5.7.3
This example uses Binet’s formula to calculate Fibonacci numbers. The doubles have to be printed with at least one decimal digit, so the output looks like this:
1.0, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 21.0, 34.0, 55.0, 89.0, 144.0, 233.0, 377.0, 610.0, 987.0, ...
-module(prog).
-export([main/0]).
fib(0) -> ok;
fib(N) ->
fib(N-1),
SQ5 = math:sqrt(5),
T1 = math:pow(0.5*(1 + SQ5),N),
T2 = math:pow(0.5*(1 - SQ5),N),
io:format("~.1f, ", [(T1-T2)/SQ5]).
main() ->
fib(16),
io:format("...~n").
Comments
]]>blog comments powered by Disqus
]]>