diff --git a/problems/lib/euler.ex b/problems/lib/euler.ex deleted file mode 100644 index 8626827..0000000 --- a/problems/lib/euler.ex +++ /dev/null @@ -1,6 +0,0 @@ -defmodule Euler do - def f() do - IO.puts("Hello, world") - end -end - diff --git a/problems/lib/euler/algebra.ex b/problems/lib/euler/algebra.ex new file mode 100644 index 0000000..e871898 --- /dev/null +++ b/problems/lib/euler/algebra.ex @@ -0,0 +1,26 @@ +defmodule Euler.Algebra do + def divisors(n) do + [ 1 | + (for i <- 2..floor(:math.sqrt(n))//1, + j = div(n, i), + j == n / i do + if i !== j, do: [i,j], else: [i] + end |> Enum.flat_map(&(&1))) ] + end + + defp fact(1, acc), do: acc + defp fact(n, acc), do: fact(n - 1, n * acc) + @doc """ + Computes n! + """ + def fact(n), do: fact(n - 1, n) + + defp digit_sum(0, acc), do: acc + defp digit_sum(n, acc), do: digit_sum(div(n, 10), rem(n, 10) + acc) + @doc """ + Returns the sum of the digits of `n`. + """ + def digit_sum(n), do: digit_sum(n, 0) +end + + diff --git a/problems/lib/problems.ex b/problems/lib/problems.ex deleted file mode 100644 index 8e5fc7b..0000000 --- a/problems/lib/problems.ex +++ /dev/null @@ -1,18 +0,0 @@ -defmodule Problems do - @moduledoc """ - Documentation for `Problems`. - """ - - @doc """ - Hello world. - - ## Examples - - iex> Problems.hello() - :world - - """ - def hello do - :world - end -end diff --git a/problems/solutions/20/solution.exs b/problems/solutions/20/solution.exs index ee20686..8cc32a9 100644 --- a/problems/solutions/20/solution.exs +++ b/problems/solutions/20/solution.exs @@ -1,15 +1,4 @@ -defmodule FactDigitSum do - defp fact(1, acc), do: acc - defp fact(n, acc), do: fact(n - 1, n * acc) - def fact(n), do: fact(n - 1, n) - - defp digit_sum(0, acc), do: acc - defp digit_sum(n, acc), do: digit_sum(div(n, 10), rem(n, 10) + acc) - def digit_sum(n), do: digit_sum(n, 0) -end - - -FactDigitSum.fact(100) -|> FactDigitSum.digit_sum() +Euler.Algebra.fact(100) +|> Euler.Algebra.digit_sum() |> IO.puts() diff --git a/problems/solutions/21/solution.exs b/problems/solutions/21/solution.exs index 4770dee..058b723 100644 --- a/problems/solutions/21/solution.exs +++ b/problems/solutions/21/solution.exs @@ -1,16 +1,5 @@ -defmodule AmicableNumbers do - def divisors(n) do - [ 1 | - (for i <- 2..floor(:math.sqrt(n))//1, - j = div(n, i), - j == n / i do - if i !== j, do: [i,j], else: [i] - end |> Enum.flat_map(&(&1))) ] - end -end - ns = for i <- 1..10_000, into: %{} do - {i, AmicableNumbers.divisors(i) |> Enum.sum()} + {i, Euler.Algebra.divisors(i) |> Enum.sum()} end for {a, b} <- ns, diff --git a/problems/solutions/23/solution.exs b/problems/solutions/23/solution.exs index ed2659e..6926673 100644 --- a/problems/solutions/23/solution.exs +++ b/problems/solutions/23/solution.exs @@ -1,21 +1,10 @@ -defmodule AmicableNumbers do - def divisors(n) do - [ 1 | - (for i <- 2..floor(:math.sqrt(n))//1, - j = div(n, i), - j == n / i do - if i !== j, do: [i,j], else: [i] - end |> Enum.flat_map(&(&1))) ] - end -end - defmodule Abundance do def is_abundant?(n) do cache = Process.get(:abundance, %{}) case cache |> Map.get(n) do nil -> - sum = AmicableNumbers.divisors(n) |> Enum.sum() + sum = Euler.Algebra.divisors(n) |> Enum.sum() is? = sum > n Process.put(:abundance, Map.put(cache, n, is?)) is?