Published at Jul 13 2018
·
0 comments

Instructions

Test suite

Solution

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.

Execute the tests with:

```
$ elixir sum_of_multiples_test.exs
```

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.

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

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

```
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
Code.load_file("sum_of_multiples.exs", __DIR__)
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
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,113 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments