Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
30
Task/Catalan-numbers/Erlang/catalan-numbers.erl
Normal file
30
Task/Catalan-numbers/Erlang/catalan-numbers.erl
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
-module(catalan).
|
||||
|
||||
-export([test/0]).
|
||||
|
||||
cat(N) ->
|
||||
factorial(2 * N) div (factorial(N+1) * factorial(N)).
|
||||
|
||||
factorial(N) ->
|
||||
fac1(N,1).
|
||||
|
||||
fac1(0,Acc) ->
|
||||
Acc;
|
||||
fac1(N,Acc) ->
|
||||
fac1(N-1, N * Acc).
|
||||
|
||||
cat_r1(0) ->
|
||||
1;
|
||||
cat_r1(N) ->
|
||||
lists:sum([cat_r1(I)*cat_r1(N-1-I) || I <- lists:seq(0,N-1)]).
|
||||
|
||||
cat_r2(0) ->
|
||||
1;
|
||||
cat_r2(N) ->
|
||||
cat_r2(N - 1) * (2 * ((2 * N) - 1)) div (N + 1).
|
||||
|
||||
test() ->
|
||||
TestList = lists:seq(0,14),
|
||||
io:format("Directly:\n~p\n",[[cat(N) || N <- TestList]]),
|
||||
io:format("1st recusive method:\n~p\n",[[cat_r1(N) || N <- TestList]]),
|
||||
io:format("2nd recusive method:\n~p\n",[[cat_r2(N) || N <- TestList]]).
|
||||
Loading…
Add table
Add a link
Reference in a new issue