-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathupto100.exs
25 lines (19 loc) · 847 Bytes
/
upto100.exs
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
defmodule Upto100 do
def go do
explore("1", 1, :+, {1, []})
end
# spillover cases
def explore(_, current_num, _, _) when current_num > 9 do
end
# exactly 100 case
def explore(concatenated_string, 9, _, {100,[]}) do
IO.puts concatenated_string
end
# all other cases
def explore(concatenated_string, current_num, last_op, {total,[]}) do
explore(concatenated_string <> to_string(current_num+1), current_num+1, Code.eval_string(concatenated_string <> to_string(current_num+1)))
explore(concatenated_string <> "+" <> to_string(current_num+1), current_num+1, Code.eval_string(concatenated_string <> "+" <> to_string(current_num+1)))
explore(concatenated_string <> "-" <> to_string(current_num+1), current_num+1, Code.eval_string(concatenated_string <> "-" <> to_string(current_num+1)))
end
end
Upto100.go