move code to Euler.Algebra
This commit is contained in:
parent
1cfb34b7f2
commit
8a108640e4
6 changed files with 30 additions and 61 deletions
|
@ -1,6 +0,0 @@
|
|||
defmodule Euler do
|
||||
def f() do
|
||||
IO.puts("Hello, world")
|
||||
end
|
||||
end
|
||||
|
26
problems/lib/euler/algebra.ex
Normal file
26
problems/lib/euler/algebra.ex
Normal file
|
@ -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
|
||||
|
||||
|
|
@ -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
|
|
@ -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()
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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?
|
||||
|
|
Loading…
Add table
Reference in a new issue