Published at May 29 2019
·
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
```

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

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
@spec to(non_neg_integer, [non_neg_integer]) :: non_neg_integer
def to(limit, factors) do
Enum.reduce(2..(limit - 1), 0, fn number, sum ->
multipe? = Enum.any?(factors, &(rem(number, &1) == 0))
if multipe?, do: sum + number, else: sum
end)
end
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,388 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments