-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproblem77.ex
52 lines (40 loc) · 1.06 KB
/
problem77.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
ExUnit.start
require Integer
defmodule Problem77 do
use ExUnit.Case
def problem77 do
find_prime_sum_greater_than(5000) |> IO.puts
end
defp find_prime_sum_greater_than n do
find_prime_sum_greater_than 10,n
end
defp find_prime_sum_greater_than k,n do
cond do
prime_sum_combinations(k) >= n ->
k
true ->
find_prime_sum_greater_than k+1, n
end
end
defp prime_sum_combinations n do
prime_sum_combinations(n,(n-1)..2 |> Enum.to_list |> Enum.filter(&:euler_helper.prime/1),0)
end
defp prime_sum_combinations n,[],n do
1
end
defp prime_sum_combinations _,[],_ do
0
end
defp prime_sum_combinations(n, [h|t], k) when h+k == n do
1 + prime_sum_combinations n, t, k
end
defp prime_sum_combinations(n, [h|t], k) when h+k < n do
prime_sum_combinations(n, [h|t], k+h) + prime_sum_combinations(n, t, k)
end
defp prime_sum_combinations(n, [h|t], k) when h+k > n do
prime_sum_combinations n,t,k
end
test "prime_sum_combinations" do
assert prime_sum_combinations(10) == 5
end
end