diff --git a/problems/21/solution.ex b/problems/21/solution.ex new file mode 100644 index 0000000..80781ae --- /dev/null +++ b/problems/21/solution.ex @@ -0,0 +1,23 @@ +defmodule AmicableNumbers do + def divisors(n) do + [ 1 | + (for i <- 2..floor(:math.sqrt(n))//1, + div(n, i) == n / i do + [i, div(n, i)] + end |> Enum.flat_map(&(&1))) ] + end +end + +ns = for i <- 1..10_000, into: %{} do + {i, AmicableNumbers.divisors(i) |> Enum.sum()} +end + +for {a, b} <- ns, + a != b, + ns[b] == a, + reduce: 0 +do + acc -> a + acc +end +|> IO.puts() +