diff --git a/Elixir/digital_sum.ex b/Elixir/digital_sum.ex
@@ -3,17 +3,17 @@ defmodule Conjecture 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()
+ _ -> :invalid_input
end
end
def run max, n_processes do
if max < 0 or n_processes <= 0 do
- invalid_input()
+ :invalid_input
else
# Spawn a new process for each starting index from 0 to ``max`
- f = fn i -> spawn fn -> counterexpl i, max, n_processes end end
- pids = Enum.map 0..max, f
+ f = fn i -> spawn fn -> get_counterexpl i, max, n_processes end end
+ pids = Enum.map 1..n_processes, f
# Send the current PID to each process
Enum.map pids, fn pid -> send pid, self() end
@@ -26,27 +26,27 @@ defmodule Conjecture do
def listen n do
receive do
- :found -> System.halt 1
:ok -> listen (n - 1)
+ msg -> msg
end
end
- def counterexpl start, max, n_processes do
+ def get_counterexpl start, max, n_processes do
receive do
parent_pid ->
- if counterexpl_loop start, max, n_processes do
- send parent_pid, :found
+ if counterexpl start, max, n_processes do
+ send parent_pid, :fail
else
send parent_pid, :ok
end
end
end
- def counterexpl_loop a, max, n_processes do
+ def counterexpl a, max, n_processes do
cond do
iter a, 0 -> true
a + n_processes <= max ->
- counterexpl_loop (a + n_processes), max, n_processes
+ counterexpl (a + n_processes), max, n_processes
true -> false
end
end
@@ -60,7 +60,7 @@ defmodule Conjecture do
end
def test a, b do
- rem (sum (a + b) - sum a - sum b), 9 == 0
+ 0 != rem(sum(a + b) - sum(a) - sum(b), 9)
end
def sum 0 do 0 end
@@ -71,8 +71,4 @@ defmodule Conjecture do
d + sum r
end
-
- def invalid_input do
- System.halt 2
- end
end