Published at Feb 25 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.

To run the tests run the command `go test`

from within the exercise directory.

If the test suite contains benchmarks, you can run these with the `--bench`

and `--benchmem`

flags:

```
go test -v --bench . --benchmem
```

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language 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.

```
package summultiples
// Source: exercism/problem-specifications
// Commit: bd2d4d9 sum-of-multiples: the factor 0 does not affect the sum of multiples of other factors
// Problem Specifications Version: 1.5.0
var varTests = []struct {
divisors []int
limit int
sum int
}{
{[]int{3, 5}, 1, 0}, // no multiples within limit
{[]int{3, 5}, 4, 3}, // one factor has multiples within limit
{[]int{3}, 7, 9}, // more than one multiple within limit
{[]int{3, 5}, 10, 23}, // more than one factor with multiples within limit
{[]int{3, 5}, 100, 2318}, // each multiple is only counted once
{[]int{3, 5}, 1000, 233168}, // a much larger limit
{[]int{7, 13, 17}, 20, 51}, // three factors
{[]int{4, 6}, 15, 30}, // factors not relatively prime
{[]int{5, 6, 8}, 150, 4419}, // some pairs of factors relatively prime and some not
{[]int{5, 25}, 51, 275}, // one factor is a multiple of another
{[]int{43, 47}, 10000, 2203160}, // much larger factors
{[]int{1}, 100, 4950}, // all numbers are multiples of 1
{[]int{}, 10000, 0}, // no factors means an empty sum
{[]int{0}, 1, 0}, // the only multiple of 0 is 0
{[]int{3, 0}, 4, 3}, // the factor 0 does not affect the sum of multiples of other factors
{[]int{2, 3, 5, 7, 11}, 10000, 39614537}, // solutions using include-exclude must extend to cardinality greater than 3
}
```

```
package summultiples
import "testing"
func TestSumMultiples(t *testing.T) {
for _, test := range varTests {
s := SumMultiples(test.limit, test.divisors...)
if s != test.sum {
t.Fatalf("Sum of multiples of %v to %d returned %d, want %d.",
test.divisors, test.limit, s, test.sum)
}
}
}
func BenchmarkSumMultiples(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range varTests {
SumMultiples(test.limit, test.divisors...)
}
}
}
```

```
package summultiples
// SumMultiples returns the sum of all the unique multiples of particular numbers up to but not including that number.
func SumMultiples(limit int, divs ...int) interface{} {
sum := 0
divisors := GetDivisors(limit, divs)
for i := 1; i < limit; i++ {
if IsMultiple(i, divisors) {
sum += i
}
}
return sum
}
// IsMultiple determines whether the given number is divisible by one of the divisors.
func IsMultiple(number int, divisors []int) bool {
for _, div := range divisors {
if number%div == 0 {
return true
}
}
return false
}
// GetDivisors returns divisors greater than 0 and less than limit.
func GetDivisors(limit int, divs []int) []int {
divisors := make([]int, 0)
for _, div := range divs {
if 0 < div && div < limit {
divisors = append(divisors, div)
}
}
return divisors
}
```

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?

## Community comments