Skip to content

Instantly share code, notes, and snippets.

@lyuehh
Created March 2, 2013 07:08
Show Gist options
  • Save lyuehh/5070009 to your computer and use it in GitHub Desktop.
Save lyuehh/5070009 to your computer and use it in GitHub Desktop.
#!/usr/bin/env escript
%% 递归版本
fib(1) -> 1;
fib(2) -> 1;
fib(N) -> fib(N-1) + fib(N-2).
%% 尾递归优化版本
fib2(N) -> fib2_iter(1,0,N).
fib2_iter(A, B, N) ->
if
N == 0 -> B;
true -> fib2_iter(A+B, A, N-1)
end.
main(_) ->
io:format("~p~n",[fib(1)]),
io:format("~p~n",[fib2(40)]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment