Skip to content

Instantly share code, notes, and snippets.

@larsbutler
Created March 25, 2017 15:35
Show Gist options
  • Save larsbutler/122256d6a99c10b879af097929bacde6 to your computer and use it in GitHub Desktop.
Save larsbutler/122256d6a99c10b879af097929bacde6 to your computer and use it in GitHub Desktop.
Erlang - Recursive Fibonacci - Θ(n)
-module(fib).
fib(Term) ->
fib(Term, 1, 0).
fib(0, _Val, Prev) ->
Prev;
fib(1, Val, _Prev) ->
Val;
fib(Term, Val, Prev) ->
fib(Term - 1, Val + Prev, Val).
main(_) ->
N = 10,
io:format("fib(~w) = ~w~n", [N, fib(N)]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment