sum_of_multiples
This commit is contained in:
27
elixir/sum-of-multiples/lib/sum_of_multiples.ex
Normal file
27
elixir/sum-of-multiples/lib/sum_of_multiples.ex
Normal file
@@ -0,0 +1,27 @@
|
||||
defmodule SumOfMultiples do
|
||||
@doc """
|
||||
Adds up all numbers from 1 to a given end number that are multiples of the factors provided.
|
||||
"""
|
||||
@spec to(non_neg_integer, [non_neg_integer]) :: non_neg_integer
|
||||
def to(limit, factors) do
|
||||
for x <- 1..(limit - 1),
|
||||
Enum.any?(factors, &(&1 != 0 && rem(x, &1) == 0 )),
|
||||
reduce: 0
|
||||
do
|
||||
acc ->
|
||||
IO.inspect(acc)
|
||||
x + acc
|
||||
end
|
||||
end
|
||||
|
||||
# def to(limit, factors) do
|
||||
# factors
|
||||
# |> Enum.flat_map(&multipliers(&1, limit))
|
||||
# |> Enum.uniq()
|
||||
# |> Enum.sum()
|
||||
# end
|
||||
|
||||
# defp multipliers(from, to) when from <= to and rem(to, from) == 0, do: Enum.take_every(from..(to - 1), from)
|
||||
# defp multipliers(from, to) when from <= to, do: Enum.take_every(from..to, from)
|
||||
# defp multipliers(_from, _to), do: []
|
||||
end
|
||||
Reference in New Issue
Block a user