exercism/elixir/sum-of-multiples/lib/sum_of_multiples.ex

28 lines
804 B
Elixir

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