Created
August 5, 2017 11:51
-
-
Save linkin-park/5039be9c3ded7fdfab2d8fcf79e01a83 to your computer and use it in GitHub Desktop.
Erlang Class - Recursion- 1
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(recursion_1_3_1). | |
-export([fib/1,dim/2]). | |
%fibonnaci | |
%0,1,2,3,4,5... | |
%0,1,1,2,3,5,... | |
fib(0)->io:fwrite([0," "]),0; | |
fib(1)->io:fwrite([1," "]),1; | |
fib(N) when N>1->K=fib(N-1)+fib(N-2),io:fwrite([K," "]),K. | |
% f(4) | |
% f(3) +f(2) | |
% f(2)+f(1) +f(1)+f(0) | |
% f(1)+f(0)+f(1) +1 +0 | |
% 1+0+1 | |
% 3 | |
% | |
% | |
%How many pieces? | |
%Define a function pieces so that pieces(N) tells you the maximum number of pieces into which you can cut a piece of paper with N straight line cuts. | |
%You can see an illustration of this problem at the top of this step. | |
%If you’d like to take this problem further, think about the 3-dimensional case. Into how many pieces can you cut a wooden block with N saw cuts? | |
%Taking it even further: What is the general problem in n dimensions? | |
%Once you have tried solving these problems, why not discuss your approaches to these exercises, and how you went about solving them, on the comments section of %this step? | |
% | |
dim(N,K) -> | |
N*maxNoOfPieces(K). | |
maxNoOfPieces(0) -> | |
1; | |
maxNoOfPieces(N) -> | |
maxNoOfPieces(N-1)+N. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment