-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproblem23.erl
75 lines (62 loc) · 1.73 KB
/
problem23.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
-module(problem23).
-include_lib("eunit/include/eunit.hrl").
-import(euler_helper,[sdivisors/1]).
-export([problem23/0]).
abundant(N) ->
sdivisors(N) > N.
abundant_numbers_upto(N) ->
lists:reverse(abundant_numbers_upto_(N)).
abundant_numbers_upto_(1) ->
[];
abundant_numbers_upto_(N) ->
Abundant = abundant(N),
if
Abundant ->
[N];
true ->
[]
end ++ abundant_numbers_upto_(N-1).
sum_of_abundant(_,[],_) ->
false;
sum_of_abundant(N,[H|_],_) when N/2 < H ->
false;
sum_of_abundant(N,[H|T],Ablist) ->
S = N - H,
gb_sets:is_member(S,Ablist) orelse sum_of_abundant(N,T,Ablist).
sum_of_not_abundant_sums_upto(N) ->
Ablist = abundant_numbers_upto(N),
AbLookupList = gb_sets:from_list(Ablist),
sum_of_not_abundant_sums_upto(N,Ablist,AbLookupList).
sum_of_not_abundant_sums_upto(1,_,_) ->
1;
sum_of_not_abundant_sums_upto(N,Ablist,AbLookupList) ->
IsAbundant = sum_of_abundant(N,Ablist,AbLookupList),
if
IsAbundant ->
0;
true ->
N
end + sum_of_not_abundant_sums_upto(N-1,Ablist,AbLookupList).
problem23() ->
sum_of_not_abundant_sums_upto(28123).
%% tests
abundant_test_() ->
[
?_assert(abundant(12)),
?_assertNot(abundant(6))
].
abundant_numbers_upto_test_() ->
[
?_assertEqual([],abundant_numbers_upto(1)),
?_assertEqual([12,18,20],abundant_numbers_upto(21))
].
sum_of_abundant_test_() ->
Ablist = abundant_numbers_upto(1024),
AbLookupList = gb_sets:from_list(Ablist),
[
?_assert(sum_of_abundant(24,Ablist,AbLookupList))
].
sum_of_not_abundant_sums_upto_test_() ->
[
?_assertEqual(25+lists:sum(lists:seq(1,23)),sum_of_not_abundant_sums_upto(25))
].