Commits

blooper committed d5c920e

Answers for problem 1 and 2 in Erlang.

Comments (0)

Files changed (2)

Erlang/problem0001_1.erl

+-module(problem0001_1).
+-export([ans/0, ans1/0]).
+
+ans() ->
+  lists:sum(
+    sets:to_list(sets:from_list(
+      lists:merge(lists:seq(3,999,3),lists:seq(5,999,5))
+    ))
+  ).
+
+ans1() ->
+  lists:sum([ X || X <- lists:seq(1,999), (X rem 3 == 0) or (X rem 5 == 0)]).

Erlang/problem0002_1.erl

+-module(problem0002_1).
+-compile(export_all).
+
+ans() -> sum(0,1,1).
+
+ans1() ->
+  lists:sum(
+    lists:filter(fun(X) -> X < 4000000 end,
+      lists:map(fun problem0002_1:fib/1, lists:seq(2,100,3))
+    )
+  ).
+
+sum(Sum,N,M) ->
+  if
+    N > 4000000 -> Sum;
+    N rem 2 == 0 -> sum(Sum+N,N+M,N);
+    true -> sum(Sum,N+M,N)
+  end.
+
+fib(0, _, _) -> 1;
+fib(1, A, _) -> A;
+fib(X, A, B) -> fib(X-1, A+B, A).
+fib(X) -> fib(X,1,1).
+