-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproblem50.erl
87 lines (74 loc) · 2.64 KB
/
problem50.erl
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
-module(problem50).
-export([problem50/0]).
-include_lib("eunit/include/eunit.hrl").
-compile(export_all).
problem50() ->
{_X,Length, Number} = longest_consecutive_prime_sum(999999),
{Number, Length}.
consecutive_prime_sum(N, PrimeList, Min) ->
consecutive_prime_sum(N, PrimeList, [], PrimeList, Min).
consecutive_prime_sum(_, [], _, _, _) ->
notfound;
consecutive_prime_sum(N, [X|_], _, _, _) when N == X ->
notfound;
consecutive_prime_sum(N, [H|T], Acc, PrimeList, Min) ->
Sum = H + lists:sum(Acc),
FirstPrime = case Acc of
[] ->
N;
[FP|_] ->
FP
end,
if
Sum == N ->
{FirstPrime, Acc ++ [H]};
Sum > N ->
[_H|ReducedPrimeList] = PrimeList,
if
FirstPrime >= Min ->
notfound;
true ->
consecutive_prime_sum(N, ReducedPrimeList, [], ReducedPrimeList, Min)
end;
true ->
consecutive_prime_sum(N, T, Acc ++ [H], PrimeList, Min)
end.
find_consecutive_prime_sums(PrimeList) ->
ReversedPrimes = lists:reverse(PrimeList),
[RH|_] = ReversedPrimes,
find_consecutive_prime_sums(ReversedPrimes, PrimeList, RH).
find_consecutive_prime_sums([], _, _) ->
[];
find_consecutive_prime_sums([H|T], PrimeList, Min) ->
io:fwrite("Prime sums for ~B~n",[H]),
Sums = consecutive_prime_sum(H,PrimeList,Min),
case Sums of
{NewMin, NewSum} ->
[NewSum] ++ find_consecutive_prime_sums(T, PrimeList, NewMin);
notfound ->
find_consecutive_prime_sums(T, PrimeList, Min)
end.
longest_consecutive_prime_sum(N) ->
PrimeList = euler_helper:calc_sieve(N),
PrimeSums = find_consecutive_prime_sums(PrimeList),
lists:foldl(fun(X,Acc) ->
Length = length(X),
{_, OldLength,_} = Acc,
if
Length > OldLength ->
{X, Length, lists:sum(X)};
true ->
Acc
end
end, {[],0,0}, PrimeSums).
% tests
consecutive_prime_sum_test_() ->
PrimeList = euler_helper:calc_sieve(1000),
[
?_assertEqual({2,[2,3,5,7,11,13]},consecutive_prime_sum(41, PrimeList,41)),
?_assertEqual(notfound,consecutive_prime_sum(43, PrimeList, 43))
].
longest_consecutive_prime_sum_test_() ->
[
{timeout, 200, ?_assertEqual({[2,3,5,7,11,13],6,41},longest_consecutive_prime_sum(100))}
].