aboutsummaryrefslogtreecommitdiff
path: root/problem21.erl
blob: 166180150e24b22caa3c84b9ced5f179d3c2a3ce (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
-module(problem21).
-export([solve/0]).

solve() ->
	L = test(1, []),
	pairs(L, 0).

test(10000, L) ->
	L;
test(A,L) ->
	D = [X || X <- lists:seq(1, erlang:trunc(math:sqrt(A))), (A rem X) =:= 0],
	E = [A div Y || Y <- D],
	F = sets:to_list(sets:from_list((D ++ E) -- [A])),
	G = lists:sum(F),
	test(A+1, [{A,G}|L]).

pairs([_], S) ->
	S;
pairs([{A,B}|T], S) ->
	M = lists:member({B, A}, T),
	if
		M ->
			pairs(T, S+A+B);
		true ->
			pairs(T, S)
	end.