Created
May 11, 2020 16:57
-
-
Save EarlOfBDE/9b2ae1c023a14eb5dafc88bd80a0cdb5 to your computer and use it in GitHub Desktop.
Functional Programming in Erlang_exercise 2.3
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 fibonacci_pieces. | |
-export [test/0]. | |
-export [fib/1]. | |
-export [pieces/1]. | |
test() -> | |
0 = fibonacci_pieces:fib(1), | |
5 = fibonacci_pieces:fib(6), | |
1 = fibonacci_pieces:pieces(0), | |
2 = fibonacci_pieces:pieces(1), | |
7 = fibonacci_pieces:pieces(3), | |
ok. | |
% @doc fib calculates the Nth Fibonacci number | |
% Because the Fibonacci sequence requires two previous values, we must first establish those two base cases | |
fib(1) -> | |
0; | |
fib(2) -> | |
1; | |
fib(N) when N>2 -> | |
fib(N-1)+fib(N-2). | |
% @doc pieces calculates the maximum number of pieces into which a piece of paper can be cut with N straight lines. | |
% "Maximum" requires that each subsequent cut intersects every previous cut (i.e. never misses a previous cut nor | |
% intersects at a vertex), so the number of pieces is always increased by the number of cuts intersected. | |
pieces(0) -> | |
% the degenerate case of zero cuts is an important base case because it represents the default case | |
% of the number of pieces in the entire original sheet of paper. | |
1; | |
pieces(N) when N>0 -> | |
pieces(N-1) + N. |
Indentation is a bit odd in this one, but the code is perfect.
LOL, you're right. Learning Erlang at the same time I'm relearning Emacs. I still don't have everything set up quite right. Thank you for taking the time to review and comment.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Indentation is a bit odd in this one, but the code is perfect.