From 934474326ac141dba9f2cca906942722e34f57a2 Mon Sep 17 00:00:00 2001 From: Caleb Webber Date: Sat, 1 Mar 2025 00:43:39 -0500 Subject: [PATCH] add problem 21 --- problems/21/solution.ex | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 problems/21/solution.ex 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() +