summaryrefslogtreecommitdiff
path: root/gcd.erl
blob: 484bd9e66b46a14e285bbb53deb9dcd45987a344 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
% Synopsis:
%
% $ escript gcd.erl 11 22 33 121
%
% or
%
% $ erl -compile gcd / erlc gcd.erl
% $ erl -noshell -run gcd main 11 22 33 121
%

-module(gcd).
-export([main/1, main/0]).
-mode(compile).

gcd2(A, 0) -> A;
gcd2(A, B) -> gcd2(B, A rem B).

gcdn(Nums) ->
  lists:foldl(fun gcd2/2, 0, Nums).

main() -> init:stop().

main([]) -> init:stop();
main(Args) ->
  Nums = lists:map(fun erlang:list_to_integer/1, Args),
  io:format("~w\n", [gcdn(Nums)]),
  init:stop().