ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# shmibs's solution

## to Sum Of Multiples in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Given a number, find the sum of all the unique multiples of particular numbers up to but not including that number.

If we list all the natural numbers below 20 that are multiples of 3 or 5, we get 3, 5, 6, 9, 10, 12, 15, and 18.

The sum of these multiples is 78.

## Running tests

Execute the tests with:

``````\$ elixir sum_of_multiples_test.exs
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

For more detailed information about the Elixir track, please see the help page.

## Source

A variation on Problem 1 at Project Euler http://projecteuler.net/problem=1

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### sum_of_multiples_test.exs

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule SumOfMultiplesTest do
use ExUnit.Case

# @tag :pending
test "sum to 1" do
assert SumOfMultiples.to(1, [3, 5]) == 0
end

@tag :pending
test "sum to 3" do
assert SumOfMultiples.to(4, [3, 5]) == 3
end

@tag :pending
test "sum to 10" do
assert SumOfMultiples.to(10, [3, 5]) == 23
end

@tag :pending
test "sum to 20" do
assert SumOfMultiples.to(20, [3, 5]) == 78
end

@tag :pending
test "sum to 100" do
assert SumOfMultiples.to(100, [3, 5]) == 2318
end

@tag :pending
test "sum to 1000" do
assert SumOfMultiples.to(1000, [3, 5]) == 233_168
end

@tag :pending
test "configurable 7, 13, 17 to 20" do
multiples = [7, 13, 17]
assert SumOfMultiples.to(20, multiples) == 51
end

@tag :pending
test "configurable 4, 6 to 15" do
multiples = [4, 6]
assert SumOfMultiples.to(15, multiples) == 30
end

@tag :pending
test "configurable 5, 6, 8 to 150" do
multiples = [5, 6, 8]
assert SumOfMultiples.to(150, multiples) == 4419
end

@tag :pending
test "configurable 43, 47 to 10000" do
multiples = [43, 47]
assert SumOfMultiples.to(10000, multiples) == 2_203_160
end
end``````
``````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, _) when limit <= 1, do: 0
def to(limit, factors) do
1..(limit - 1)
|> Stream.filter( &(is_multiple? &1, factors) )
|> Enum.sum
end

defp is_multiple?(num, factors), do: Enum.any? factors, &(rem(num, &1) == 0)

end``````