diff --git a/Elixir/digital_sum.ex b/Elixir/digital_sum.ex
@@ -1,13 +1,5 @@
defmodule Conjecture do
- def main do
- case System.argv() |> Enum.map(fn s -> Integer.parse s end) do
- [{max, ""}, {n_processes, ""}] -> run max, n_processes
- [{max, ""}] -> run max, 1
- _ -> :invalid_input
- end
- end
-
- def run max, n_processes do
+ def main max, n_processes do
if max < 0 or n_processes <= 0 do
:invalid_input
else
@@ -18,44 +10,33 @@ defmodule Conjecture do
f = fn i -> spawn fn -> counterexpl i, max, n_processes, parent_id end end
Enum.map 0..(n_processes - 1), f
- listen n_processes, %{}
+ listen n_processes
end
end
# Listen for messages on all processes
- def listen 0, _ do :ok end
+ def listen 0 do :ok end
- def listen n, sums_cache do
+ def listen n do
receive do
- :ok -> listen (n - 1), sums_cache
+ :ok -> listen (n - 1)
:fail -> :fail
- {:get, child_id, m} ->
- if Map.has_key? sums_cache, m do
- send child_id, {:found, sums_cache[m]}
- else
- send child_id, :notfound
- end
- listen n, sums_cache
- {:put, m, sum_m} ->
- listen n, Map.put(sums_cache, m, sum_m)
end
end
def counterexpl a, max, step, parent_id do
cond do
- iter a, 0 -> send parent_id, :fail
+ iter a, a -> send parent_id, :fail
a + step <= max ->
counterexpl (a + step), max, step, parent_id
true -> send parent_id, :ok
end
end
+ def iter a, 0 do test a, 0 end
+
def iter a, b do
- cond do
- test a, b -> true
- b + 1 < a -> iter a, b + 1
- true -> false
- end
+ test(a, b) || iter(a, b - 1)
end
def test a, b do
@@ -68,7 +49,6 @@ defmodule Conjecture do
end
def lookup n do
-
case :ets.lookup(:sums_cache, n) do
[{_, sum_n}] -> sum_n
[] ->