Initial Commit

This commit is contained in:
2016-08-13 18:20:14 -05:00
commit 50f4a86fd8
408 changed files with 15301 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
# Sum Of Multiples
Write a program that, given a number, can find the sum of all the multiples of particular numbers up to but not including that number.
If we list all the natural numbers up to but not including 15 that are
multiples of either 3 or 5, we get 3, 5, 6 and 9, 10, and 12.
The sum of these multiples is 45.
Write a program that can find the sum of the multiples of a given set of
numbers.
To run the tests simply run the command `go test` in the exercise directory.
If the test suite contains benchmarks, you can run these with the `-bench`
flag:
go test -bench .
For more detailed info about the Go track see the [help
page](http://help.exercism.io/getting-started-with-go.html).
## Source
A variation on Problem 1 at Project Euler [view source](http://projecteuler.net/problem=1)

View File

@@ -0,0 +1,65 @@
package summultiples
import "testing"
var test35 = []struct {
limit int
sum int
}{
{1, 0},
{4, 3},
{10, 23},
{1000, 233168},
}
var varTests = []struct {
divisors []int
limit int
sum int
}{
{[]int{7, 13, 17}, 20, 51},
{[]int{43, 47}, 10000, 2203160},
{[]int{5, 10, 12}, 10000, 13331672},
{[]int{1, 1}, 10000, 49995000},
{[]int{}, 10000, 0},
}
func Test35(t *testing.T) {
sum35 := MultipleSummer(3, 5)
for _, test := range test35 {
s := sum35(test.limit)
if s != test.sum {
t.Fatalf("Sum to %d returned %d, want %d.", test.limit, s, test.sum)
}
}
}
func TestVar(t *testing.T) {
for _, test := range varTests {
sv := MultipleSummer(test.divisors...)
s := sv(test.limit)
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 Benchmark35(b *testing.B) {
sum35 := MultipleSummer(3, 5)
b.ResetTimer() // bench just the sum function
for i := 0; i < b.N; i++ {
for _, test := range test35 {
sum35(test.limit)
}
}
}
func BenchmarkVar(b *testing.B) {
// bench combined time to bind sum function and call it.
for i := 0; i < b.N; i++ {
for _, test := range varTests {
MultipleSummer(test.divisors...)(test.limit)
}
}
}

View File

@@ -0,0 +1,27 @@
package summultiples
// MultipleSummer takes a variable number of ints
// and returns a function that, when given a limit
// returns the sum of the multiples up to that limit
func MultipleSummer(mults ...int) func(int) int {
return func(limit int) int {
var ret int
for i := range mults {
sumMult := mults[i]
for sumMult < limit {
var skip bool
for j := 0; j < i && !skip; j++ {
// Make sure that we haven't already added sumMult
if sumMult%mults[j] == 0 {
skip = true
}
}
if !skip {
ret += sumMult
}
sumMult += mults[i]
}
}
return ret
}
}