summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-27 10:58:13 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-27 10:58:13 +0200
commitb81aee1ab59df82b23ca9cc4f771f9a6a309780c (patch)
tree22bb96896cdf180c55a46d3984cb6717f131a7d5
parentacbc22c498a5e58b810f6a3838fa85e597b7f4d9 (diff)
Add pythagorean triplet
-rw-r--r--go/pythagorean-triplet/README.md34
-rw-r--r--go/pythagorean-triplet/pythagorean_triplet.go6
-rw-r--r--go/pythagorean-triplet/pythagorean_triplet_test.go73
3 files changed, 113 insertions, 0 deletions
diff --git a/go/pythagorean-triplet/README.md b/go/pythagorean-triplet/README.md
new file mode 100644
index 0000000..861ffc7
--- /dev/null
+++ b/go/pythagorean-triplet/README.md
@@ -0,0 +1,34 @@
+# Pythagorean Triplet
+
+There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product a * b * c.
+
+A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for
+which,
+
+```
+a**2 + b**2 = c**2
+```
+
+For example,
+
+```
+3**2 + 4**2 = 9 + 16 = 25 = 5**2.
+```
+
+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 9 at Project Euler [http://projecteuler.net/problem=9](http://projecteuler.net/problem=9)
+
+## 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/pythagorean-triplet/pythagorean_triplet.go b/go/pythagorean-triplet/pythagorean_triplet.go
new file mode 100644
index 0000000..8a6a9cd
--- /dev/null
+++ b/go/pythagorean-triplet/pythagorean_triplet.go
@@ -0,0 +1,6 @@
+package pythagorean
+
+type Triplet [3]int
+
+func Range(min, max int) []Triplet { return nil }
+func Sum(p int) []Triplet { return nil }
diff --git a/go/pythagorean-triplet/pythagorean_triplet_test.go b/go/pythagorean-triplet/pythagorean_triplet_test.go
new file mode 100644
index 0000000..0af626a
--- /dev/null
+++ b/go/pythagorean-triplet/pythagorean_triplet_test.go
@@ -0,0 +1,73 @@
+package pythagorean
+
+// Use this type definition,
+//
+// type Triplet [3]int
+//
+// and implement two functions,
+//
+// Range(min, max int) []Triplet
+// Sum(p int) []Triplet
+//
+// Range returns a list of all Pythagorean triplets with sides in the
+// range min to max inclusive.
+//
+// Sum returns a list of all Pythagorean triplets where the sum a+b+c
+// (the perimeter) is equal to p.
+//
+// The three elements of each returned triplet must be in order,
+// t[0] <= t[1] <= t[2], and the list of triplets must be in lexicographic
+// order.
+
+import (
+ "reflect"
+ "testing"
+)
+
+var rangeTests = []struct {
+ min, max int
+ ts []Triplet
+}{
+ {1, 10, []Triplet{{3, 4, 5}, {6, 8, 10}}},
+ {11, 20, []Triplet{{12, 16, 20}}},
+}
+
+func TestRange(t *testing.T) {
+ for _, test := range rangeTests {
+ ts := Range(test.min, test.max)
+ if !reflect.DeepEqual(ts, test.ts) {
+ t.Fatalf("Range(%d, %d) = %v, want %v",
+ test.min, test.max, ts, test.ts)
+ }
+ }
+}
+
+var sumTests = []struct {
+ sum int
+ ts []Triplet
+}{
+ {180, []Triplet{{18, 80, 82}, {30, 72, 78}, {45, 60, 75}}},
+ {1000, []Triplet{{200, 375, 425}}},
+}
+
+func TestSum(t *testing.T) {
+ for _, test := range sumTests {
+ ts := Sum(test.sum)
+ if !reflect.DeepEqual(ts, test.ts) {
+ t.Fatalf("Sum(%d) = %v, want %v",
+ test.sum, ts, test.ts)
+ }
+ }
+}
+
+func BenchmarkRange(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Range(1, 100)
+ }
+}
+
+func BenchmarkSum(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Sum(1000)
+ }
+}