Published at Oct 18 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.

Go through the setup instructions for Kotlin to install the necessary dependencies:

https://exercism.io/tracks/kotlin/installation

Execute the tests with:

```
$ gradlew test
```

Use

`gradlew.bat`

if you're on Windows

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

Once you get a test passing, you can enable the next one by removing the
`@Ignore`

annotation.

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.junit.Ignore
import org.junit.Test
import kotlin.test.assertEquals
class SumOfMultiplesTest {
@Test
fun `multiples of 3 or 5 up to 1`() {
assertEquals(0, SumOfMultiples.sum(setOf(3, 5), 1))
}
@Test
@Ignore
fun `multiples of 3 or 5 up to 4`() {
assertEquals(3, SumOfMultiples.sum(setOf(3, 5), 4))
}
@Test
@Ignore
fun `multiples of 3 up to 7`() {
assertEquals(9, SumOfMultiples.sum(setOf(3), 7))
}
@Test
@Ignore
fun `multiples of 3 or 5 up to 10`() {
assertEquals(23, SumOfMultiples.sum(setOf(3, 5), 10))
}
@Test
@Ignore
fun `multiples of 3 or 5 up to 100`() {
assertEquals(2318, SumOfMultiples.sum(setOf(3, 5), 100))
}
@Test
@Ignore
fun `multiples of 3 or 5 up to 1000`() {
assertEquals(233168, SumOfMultiples.sum(setOf(3, 5), 1000))
}
@Test
@Ignore
fun `multiples of 7, 13 or 17 up to 20`() {
assertEquals(51, SumOfMultiples.sum(setOf(7, 13, 17), 20))
}
@Test
@Ignore
fun `multiples of 4 or 6 up to 15`() {
assertEquals(30, SumOfMultiples.sum(setOf(4, 6), 15))
}
@Test
@Ignore
fun `multiples of 5, 6 or 8 up to 150`() {
assertEquals(4419, SumOfMultiples.sum(setOf(5, 6, 8), 150))
}
@Test
@Ignore
fun `multiples of 5 or 25 up to 51`() {
assertEquals(275, SumOfMultiples.sum(setOf(5, 25), 51))
}
@Test
@Ignore
fun `multiples of 43 or 47 up to 10000`() {
assertEquals(2203160, SumOfMultiples.sum(setOf(43, 47), 10000))
}
@Test
@Ignore
fun `multiples of 1 up to 100`() {
assertEquals(4950, SumOfMultiples.sum(setOf(1), 100))
}
@Test
@Ignore
fun `multiples of an empty set up to 10000`() {
assertEquals(0, SumOfMultiples.sum(emptySet(), 10000))
}
}
```

```
class SumOfMultiples {
companion object {
fun sum(multiplierSet: Set<Int>, targetNum: Int): Int {
if (multiplierSet.isEmpty()) return 0
return (1 until targetNum).filter { num -> multiplierSet.any { num.rem(it) == 0 } }.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,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments