summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-25 04:01:30 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-25 04:01:30 +0200
commitff662e72997f80c940ddb6f08b032a05f8f4f285 (patch)
treec198b4c878e43dd099efba7bacfee675acafcb87
parentfd484ec9cb380646a2b4017e64de2be16c04ed2a (diff)
difference-of-squares
-rw-r--r--go/difference-of-squares/README.md34
-rw-r--r--go/difference-of-squares/difference_of_squares.go21
-rw-r--r--go/difference-of-squares/difference_of_squares_test.go54
3 files changed, 109 insertions, 0 deletions
diff --git a/go/difference-of-squares/README.md b/go/difference-of-squares/README.md
new file mode 100644
index 0000000..8022e2a
--- /dev/null
+++ b/go/difference-of-squares/README.md
@@ -0,0 +1,34 @@
+# Difference Of Squares
+
+Find the difference between the sum of the squares and the square of the sums of the first N natural numbers.
+
+The square of the sum of the first ten natural numbers is,
+
+ (1 + 2 + ... + 10)**2 = 55**2 = 3025
+
+The sum of the squares of the first ten natural numbers is,
+
+ 1**2 + 2**2 + ... + 10**2 = 385
+
+Hence the difference between the square of the sum of the first
+ten natural numbers and the sum of the squares is 2640:
+
+ 3025 - 385 = 2640
+
+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://exercism.io/languages/go).
+
+## Source
+
+Problem 6 at Project Euler [http://projecteuler.net/problem=6](http://projecteuler.net/problem=6)
+
+## Submitting Incomplete Problems
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
diff --git a/go/difference-of-squares/difference_of_squares.go b/go/difference-of-squares/difference_of_squares.go
new file mode 100644
index 0000000..3d392cd
--- /dev/null
+++ b/go/difference-of-squares/difference_of_squares.go
@@ -0,0 +1,21 @@
+package diffsquares
+
+func SquareOfSums(n int) int {
+ var sum int
+ for i := 1; i <= n; i++ {
+ sum += i
+ }
+ return sum * sum
+}
+
+func SumOfSquares(n int) int {
+ var sum int
+ for i := 1; i <= n; i++ {
+ sum += i * i
+ }
+ return sum
+}
+
+func Difference(n int) int {
+ return SquareOfSums(n) - SumOfSquares(n)
+}
diff --git a/go/difference-of-squares/difference_of_squares_test.go b/go/difference-of-squares/difference_of_squares_test.go
new file mode 100644
index 0000000..d3fe30f
--- /dev/null
+++ b/go/difference-of-squares/difference_of_squares_test.go
@@ -0,0 +1,54 @@
+package diffsquares
+
+import "testing"
+
+var tests = []struct{ n, sqOfSums, sumOfSq int }{
+ {5, 225, 55},
+ {10, 3025, 385},
+ {100, 25502500, 338350},
+}
+
+func TestSquareOfSums(t *testing.T) {
+ for _, test := range tests {
+ if s := SquareOfSums(test.n); s != test.sqOfSums {
+ t.Fatalf("SquareOfSums(%d) = %d, want %d", test.n, s, test.sqOfSums)
+ }
+ }
+}
+
+func TestSumOfSquares(t *testing.T) {
+ for _, test := range tests {
+ if s := SumOfSquares(test.n); s != test.sumOfSq {
+ t.Fatalf("SumOfSquares(%d) = %d, want %d", test.n, s, test.sumOfSq)
+ }
+ }
+}
+
+func TestDifference(t *testing.T) {
+ for _, test := range tests {
+ want := test.sqOfSums - test.sumOfSq
+ if s := Difference(test.n); s != want {
+ t.Fatalf("Difference(%d) = %d, want %d", test.n, s, want)
+ }
+ }
+}
+
+// Benchmark functions on just a single number (100, from the original PE problem)
+// to avoid overhead of iterating over tests.
+func BenchmarkSquareOfSums(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ SquareOfSums(100)
+ }
+}
+
+func BenchmarkSumOfSquares(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ SumOfSquares(100)
+ }
+}
+
+func BenchmarkDifference(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Difference(100)
+ }
+}