Created
May 9, 2020 20:08
-
-
Save pppillai/5af9e5313798c0212d3f0b119ffaa365 to your computer and use it in GitHub Desktop.
This file contains 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
-module(fibandcuts). | |
-export([factorial/1, fibonacci/1, pieces/1]). | |
-export([test_pieces/0,test_fibonacci/0]). | |
-author("Pradeep Pillai"). | |
%Factorial of N | |
%1 * 2 * 3 * (N-1) | |
%Factorial of 0 is 0 | |
factorial(0) -> | |
1; | |
factorial(N) when N > 0 -> | |
N * factorial(N-1). | |
fibonacci(0) -> | |
0; | |
fibonacci(1) -> | |
1; | |
fibonacci(N) when N > 1 -> | |
fibonacci(N - 2) + fibonacci(N - 1). | |
%when no cuts one piece of paper | |
%when on cut two piece so 1 + pieces(0) which evaluates to 1 + 1 | |
%when 2 cuts so 2 + piece(1) = 2 + 1 + piece(0) = 2+1+1 = 4 | |
%when 3 cuts so 3 + piece(2) = 3 + 2 + piece(1) = 3 + 2 + 1 + piece(0) = 3 + 2 + 1 + 1 = 7 | |
pieces(0) -> | |
1; | |
pieces(1) -> | |
1 + pieces(0); | |
pieces(N) when N > 0-> | |
N + pieces(N - 1). | |
%%tests | |
test_pieces() -> | |
(pieces(0) == 1) andalso (pieces(1) == 2) andalso (pieces(2) == 4). | |
test_fibonacci() -> | |
(fibonacci(0) == 0) andalso (fibonacci(1) == 1) andalso (fibonacci(3) == 2). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very Good!