Skip to content

Commit d942298

Browse files
author
Bruno Ferreira
committed
Adds simple solution in kotlin for the sum of multiples problem
1 parent 7d12b54 commit d942298

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

kotlin/sum-of-multiples/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Sum Of Multiples
2+
3+
Given a number, find the sum of all the unique multiples of particular numbers up to
4+
but not including that number.
5+
6+
If we list all the natural numbers below 20 that are multiples of 3 or 5,
7+
we get 3, 5, 6, 9, 10, 12, 15, and 18.
8+
9+
The sum of these multiples is 78.
10+
## Source
11+
12+
A variation on Problem 1 at Project Euler [http://projecteuler.net/problem=1](http://projecteuler.net/problem=1)
13+
14+
## Submitting Incomplete Solutions
15+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

kotlin/sum-of-multiples/build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
buildscript {
2+
ext.kotlin_version = '1.2.40'
3+
repositories {
4+
mavenCentral()
5+
}
6+
dependencies {
7+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
8+
}
9+
}
10+
11+
apply plugin: 'kotlin'
12+
13+
repositories {
14+
mavenCentral()
15+
}
16+
17+
dependencies {
18+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
19+
20+
testCompile 'junit:junit:4.12'
21+
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
22+
}
23+
test {
24+
testLogging {
25+
exceptionFormat = 'full'
26+
events = ["passed", "failed", "skipped"]
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object SumOfMultiples {
2+
fun sum(multiplesOf: Set<Int>, upTo: Int): Int {
3+
return (1..(upTo - 1)).filter { n ->
4+
multiplesOf.indexOfFirst { m ->
5+
n % m == 0
6+
} > -1
7+
}.sum()
8+
}
9+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import org.junit.Ignore
2+
import org.junit.Test
3+
import kotlin.test.assertEquals
4+
5+
class SumOfMultiplesTest {
6+
7+
@Test
8+
fun `multiples of 3 or 5 up to 1`() {
9+
assertEquals(0, SumOfMultiples.sum(setOf(3, 5), 1))
10+
}
11+
12+
@Test
13+
fun `multiples of 3 or 5 up to 4`() {
14+
assertEquals(3, SumOfMultiples.sum(setOf(3, 5), 4))
15+
}
16+
17+
@Test
18+
fun `multiples of 3 up to 7`() {
19+
assertEquals(9, SumOfMultiples.sum(setOf(3), 7))
20+
}
21+
22+
@Test
23+
fun `multiples of 3 or 5 up to 10`() {
24+
assertEquals(23, SumOfMultiples.sum(setOf(3, 5), 10))
25+
}
26+
27+
@Test
28+
fun `multiples of 3 or 5 up to 100`() {
29+
assertEquals(2318, SumOfMultiples.sum(setOf(3, 5), 100))
30+
}
31+
32+
@Test
33+
fun `multiples of 3 or 5 up to 1000`() {
34+
assertEquals(233168, SumOfMultiples.sum(setOf(3, 5), 1000))
35+
}
36+
37+
@Test
38+
fun `multiples of 7, 13 or 17 up to 20`() {
39+
assertEquals(51, SumOfMultiples.sum(setOf(7, 13, 17), 20))
40+
}
41+
42+
@Test
43+
fun `multiples of 4 or 6 up to 15`() {
44+
assertEquals(30, SumOfMultiples.sum(setOf(4, 6), 15))
45+
}
46+
47+
@Test
48+
fun `multiples of 5, 6 or 8 up to 150`() {
49+
assertEquals(4419, SumOfMultiples.sum(setOf(5, 6, 8), 150))
50+
}
51+
52+
@Test
53+
fun `multiples of 5 or 25 up to 51`() {
54+
assertEquals(275, SumOfMultiples.sum(setOf(5, 25), 51))
55+
}
56+
57+
@Test
58+
fun `multiples of 43 or 47 up to 10000`() {
59+
assertEquals(2203160, SumOfMultiples.sum(setOf(43, 47), 10000))
60+
}
61+
62+
@Test
63+
fun `multiples of 1 up to 100`() {
64+
assertEquals(4950, SumOfMultiples.sum(setOf(1), 100))
65+
}
66+
67+
@Test
68+
fun `multiples of an empty set up to 10000`() {
69+
assertEquals(0, SumOfMultiples.sum(emptySet(), 10000))
70+
}
71+
72+
}

0 commit comments

Comments
 (0)