Skip to content

Instantly share code, notes, and snippets.

@yueyoum
Created May 23, 2014
Embed
What would you like to do?
-module(fibo).
-export([start/1]).
-vsn(1.1).
start(Num) ->
ResultList = start(Num, 0, []),
Result = lists:sum(ResultList),
io:format("~p~n", [ResultList]),
io:format("~p~n", [Result]),
ok.
start(0, _, Result) ->
[0 | Result];
start(1, _, Result) ->
[1 | Result];
start(Num, 0, Result) ->
start(Num, 1, [0 | Result]);
start(Num, 1, Result) ->
start(Num, 2, [1 | Result]);
start(Num, Acc, [H1, H2 | _Tail] = Result) when Acc =< Num ->
start(Num, Acc + 1, [H1 + H2 | Result]);
start(_, _, Result) ->
lists:reverse(Result).
%% wang[12:39][/tmp]$ erl
%% Erlang R16B02 (erts-5.10.3) [source] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
%%
%% Eshell V5.10.3 (abort with ^G)
%% 1> c(fibo).
%% {ok,fibo}
%% 2> timer:tc(fibo, start, [40]).
%% [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,
%% 17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309,
%% 3524578,5702887,9227465,14930352,24157817,39088169,63245986,102334155]
%% 267914295
%% {502,ok}
%% 3>
%% 502 microseconds = 0.5 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment