-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproblem75.ex
62 lines (52 loc) · 1.38 KB
/
problem75.ex
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
ExUnit.start
require Integer
defmodule Problem75 do
use ExUnit.Case
def problem75 do
triplet_sums(1,1,1,HashDict.new) |>
Dict.values |>
Enum.count(&(&1 == 1)) |>
IO.puts
end
defp triplet_sums(m,_,_,sum_count_dict) when m > 1500000 do
sum_count_dict
end
defp triplet_sums(_,_,k,sum_count_dict) when k > 1500000 do
sum_count_dict
end
defp triplet_sums(m,n,_k,sum_count_dict) when m <= n do
triplet_sums m+1,1,1,sum_count_dict
end
defp triplet_sums(m,n,k,sum_count_dict) when rem(m-n,2) == 0 do
triplet_sums m,n+1,k,sum_count_dict
end
defp triplet_sums(m,n,k,sum_count_dict) when k*(m*m-n*n) + k*2*m*n + k*(m*m+n*n) > 1500000 do
case k do
1 ->
triplet_sums m+1,1,1,sum_count_dict
_ ->
triplet_sums m,n+1,1,sum_count_dict
end
end
defp triplet_sums m,n,k,sum_count_dict do
case :euler_helper.gcd(m,n) do
1 ->
new_dict = insert_triplet_sum m,n,k,sum_count_dict
triplet_sums m,n,k+1,new_dict
_ ->
triplet_sums m,n+1,1,sum_count_dict
end
end
defp insert_triplet_sum m,n,k,sum_count_dict do
a = k*(m*m-n*n)
b = k*2*m*n
c = k*(m*m+n*n)
sum = a + b + c
case Dict.fetch(sum_count_dict, sum) do
{:ok, num_of_sums} ->
Dict.put(sum_count_dict, sum, num_of_sums+1)
_ ->
Dict.put(sum_count_dict, sum, 1)
end
end
end