We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
-module(solution).-export([main/0]).%% Factorial with tail recursionfac(N)->fac(N,1).fac(0,Acc)->Acc;fac(N,Acc)whenN>0->fac(N-1,N*Acc).%% Calculates the value of a column in a row.column(N,M)->fac(N)div(fac(M)*fac(N-M)).%% Builds a rowbuild_row(N)->build_row(N,1).build_row(N,M)whenNrem2==1,M==Ndiv2+1->[column(N-1,M-1)];build_row(N,M)whenNrem2==0,M-1==Ndiv2->[];build_row(N,M)whenN>M->C=column(N-1,M-1),[C]++build_row(N,M+1)++[C].%% Prints a rowprint_row([])->io:format("~n");print_row([X|Xs])->io:format("~p ",[X]),print_row(Xs).%% Builds the trianglepascal(N,M)whenN<M->ok;pascal(N,I)->R=build_row(I),print_row(R),pascal(N,I+1).main()->{ok,[N]}=io:fread("","~d"),pascal(N,1).
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Pascal's Triangle
You are viewing a single comment's thread. Return to all comments →
Erlang: