add problem 21
This commit is contained in:
parent
77445e8fbe
commit
934474326a
1 changed files with 23 additions and 0 deletions
23
problems/21/solution.ex
Normal file
23
problems/21/solution.ex
Normal 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()
|
||||||
|
|
Loading…
Add table
Reference in a new issue