add problem 21

This commit is contained in:
Caleb Webber 2025-03-01 00:43:39 -05:00
parent 77445e8fbe
commit 934474326a

23
problems/21/solution.ex Normal file
View file

@ -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()