-module euler10 -export find_sum new_primer List receive get_list Pid

 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
37
38
39
40
41
42
43
-module(euler10).
-export([find_sum/1]).
new_primer(List) ->
receive
{get_list, Pid} ->
N_L = List,
Pid ! {primes_list, List} ;
{add_prime, X} ->
N_L = lists:append(List,[X])
end,
new_primer(N_L).
get_primes() ->
Primer_active = lists:member(primer, registered()),
if
Primer_active -> true;
true ->
P = spawn(fun() -> new_primer([]) end),
register(primer, P)
end,
primer ! {get_list, self()},
receive
{primes_list, L} -> L
end.
find_primes(N, C) when C >= N-> get_primes();
find_primes(N, C) ->
Primes = get_primes(),
Is_prime = lists:all(fun (X) -> C rem X /= 0 end, Primes ),
if
Is_prime ->
primer ! {add_prime, C};
true -> true
end,
find_primes(N, C+1).
find_primes(N) -> find_primes(N,2).
find_sum(N) ->
Primes = find_primes(N),
lists:sum(Primes).