Initial Commit
This commit is contained in:
30
go/pythagorean-triplet/README.md
Normal file
30
go/pythagorean-triplet/README.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# 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://help.exercism.io/getting-started-with-go.html).
|
||||
|
||||
## Source
|
||||
|
||||
Problem 9 at Project Euler [view source](http://projecteuler.net/problem=9)
|
44
go/pythagorean-triplet/pythagorean.go
Normal file
44
go/pythagorean-triplet/pythagorean.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package pythagorean
|
||||
|
||||
import "math"
|
||||
|
||||
// Triplet just holds three ints
|
||||
type Triplet struct {
|
||||
a, b, c int
|
||||
}
|
||||
|
||||
// Range finds all pythagorean triplets
|
||||
// from min to max
|
||||
func Range(min, max int) []Triplet {
|
||||
var ret []Triplet
|
||||
for a := min; a <= max; a++ {
|
||||
for b := a + 1; b <= max; b++ {
|
||||
cSq := float64(a*a + b*b)
|
||||
c := int(math.Sqrt(cSq))
|
||||
if c >= min && c <= max && isTriplet(a, b, c) {
|
||||
ret = append(ret, Triplet{a, b, c})
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Sum finds all triplets that sum up to total
|
||||
func Sum(total int) []Triplet {
|
||||
var ret []Triplet
|
||||
|
||||
// No side of a triangle can be over 1/2 of the total
|
||||
for a := 1; a <= (total / 2); a++ {
|
||||
for b := a; b <= (total / 2); b++ {
|
||||
c := total - a - b
|
||||
if isTriplet(a, b, c) {
|
||||
ret = append(ret, Triplet{a, b, c})
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func isTriplet(a, b, c int) bool {
|
||||
return (a*a + b*b) == c*c
|
||||
}
|
73
go/pythagorean-triplet/pythagorean_triplet_test.go
Normal file
73
go/pythagorean-triplet/pythagorean_triplet_test.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user