Created
March 2, 2013 07:08
-
-
Save lyuehh/5070009 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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