aboutsummaryrefslogtreecommitdiff
path: root/problem2.erl
blob: 34f8a1e8db333ca0b7f4f01c49b9ca19faf1563d (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
29
30
31
32
33
34
35
36
-module(problem2).
-export([solve/0]).

solve() ->
	S = spawn(fun() -> loop([2]) end),
	work(S, 4000000, 1, 2).

work(S, M, X, Y) ->
	Z = X + Y,
	if
		Z > M ->
			S ! {self(), done};
		true ->
			S ! {self(), Z}
	end,
	receive
		continue ->
			work(S, M, Y, Z);
		R ->
			R
	end.

loop(L) ->
	receive
		{From , X} when is_integer(X) ->
			if
				X rem 2 =:= 0 ->
					From ! continue,
					loop(L++[X]);
				true ->
					From ! continue,
					loop(L)
			end;
		{From, done} ->
			From ! lists:sum(L)
	end.