diff --git a/problem1/smith.exs b/problem1/smith.exs new file mode 100644 index 0000000..473251e --- /dev/null +++ b/problem1/smith.exs @@ -0,0 +1,19 @@ +defmodule Euler do + defp _multiple_of_3_or_5(num) do + cond do + rem(num, 3) == 0 -> true + rem(num, 5) == 0 -> true + true -> + false + end + end + def run(num) do + Enum.to_list(1..(num-1)) + |> Enum.filter( &(_multiple_of_3_or_5(&1)) ) + |> Enum.sum + end +end + +# IO.puts Euler.run(10) +IO.puts Euler.run(1000) +# IO.puts Euler.run(10_000_000) \ No newline at end of file diff --git a/problem2/smith.exs b/problem2/smith.exs new file mode 100644 index 0000000..39a30fa --- /dev/null +++ b/problem2/smith.exs @@ -0,0 +1,18 @@ +defmodule Euler do + + defp _lperms([]), do: [[]] + defp _lperms([head|tail]) do + for new_head <- [head|tail], + # get all perms of list - past perm + # i.e [[0,1,2], [1,0,2]] -- [[0,1,2]] + new_tail <- _lperms([head|tail] -- [new_head]), + do: [new_head | new_tail] + end + + def run(range) do + _lperms(Enum.to_list(range)) + end +end + +IO.inspect Euler.run(0..9) + |> Enum.at(1_000_000 - 1 ) # zero correction