Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.55 KB | None | 0 0
  1. -module(fourth).
  2. -export([fib/1, howMany/1, howMany3d/1]).
  3.  
  4. fib(1) -> 0;
  5. fib(2) -> 1;
  6. fib(N) when N>0 ->
  7.     fib(N-1) + fib(N-2).
  8.  
  9. %calling fib(4), calls fib(3) and fib(2)
  10. %The call to fib(3) calls fib(2) and fib(1),
  11. %which returns 1 and 0 respectively.
  12. %The call to fib(2) on behalf of fib(4) returns
  13. %1. So we have 1 + 1, which is the fourth number in
  14. %the fibonnaci sequence.
  15.  
  16.  
  17.    
  18. howMany(0) -> 1;
  19.  
  20. howMany(N) when N > 0 ->
  21.     N + (howMany(N-1)).
  22.  
  23. % how many 3d.
  24. howMany3d(0) -> 1;
  25. howMany3d3d(N) when N>0 ->
  26.     howMany3d(N-1) + howMany(N-1).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement