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

Published at Jan 21 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.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test`

in the exercise directory.

For more detailed info about the Scala track 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.

```
import org.scalatest.{Matchers, FunSuite}
/** @version 1.2.0 */
class SumOfMultiplesTest extends FunSuite with Matchers {
test("multiples of 3 or 5 up to 1") {
SumOfMultiples.sum(Set(3, 5), 1) should be(0)
}
test("multiples of 3 or 5 up to 4") {
pending
SumOfMultiples.sum(Set(3, 5), 4) should be(3)
}
test("multiples of 3 up to 7") {
pending
SumOfMultiples.sum(Set(3), 7) should be(9)
}
test("multiples of 3 or 5 up to 10") {
pending
SumOfMultiples.sum(Set(3, 5), 10) should be(23)
}
test("multiples of 3 or 5 up to 100") {
pending
SumOfMultiples.sum(Set(3, 5), 100) should be(2318)
}
test("multiples of 3 or 5 up to 1000") {
pending
SumOfMultiples.sum(Set(3, 5), 1000) should be(233168)
}
test("multiples of 7, 13 or 17 up to 20") {
pending
SumOfMultiples.sum(Set(7, 13, 17), 20) should be(51)
}
test("multiples of 4 or 6 up to 15") {
pending
SumOfMultiples.sum(Set(4, 6), 15) should be(30)
}
test("multiples of 5, 6 or 8 up to 150") {
pending
SumOfMultiples.sum(Set(5, 6, 8), 150) should be(4419)
}
test("multiples of 5 or 25 up to 51") {
pending
SumOfMultiples.sum(Set(5, 25), 51) should be(275)
}
test("multiples of 43 or 47 up to 10000") {
pending
SumOfMultiples.sum(Set(43, 47), 10000) should be(2203160)
}
test("multiples of 1 up to 100") {
pending
SumOfMultiples.sum(Set(1), 100) should be(4950)
}
test("multiples of an empty list up to 10000") {
pending
SumOfMultiples.sum(Set(), 10000) should be(0)
}
}
```

```
object SumOfMultiples {
def sum(factors: Set[Int], limit: Int): Int =
(for {factor <- factors
factor <- factor until limit by factor
} yield factor).sum
}
```

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,423 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments