Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(fourth).
- -export([fib/1, howMany/1, howMany3d/1]).
- fib(1) -> 0;
- fib(2) -> 1;
- fib(N) when N>0 ->
- fib(N-1) + fib(N-2).
- %calling fib(4), calls fib(3) and fib(2)
- %The call to fib(3) calls fib(2) and fib(1),
- %which returns 1 and 0 respectively.
- %The call to fib(2) on behalf of fib(4) returns
- %1. So we have 1 + 1, which is the fourth number in
- %the fibonnaci sequence.
- howMany(0) -> 1;
- howMany(N) when N > 0 ->
- N + (howMany(N-1)).
- % how many 3d.
- howMany3d(0) -> 1;
- howMany3d3d(N) when N>0 ->
- howMany3d(N-1) + howMany(N-1).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement