# artemkorsakov's solution

## to Pythagorean Triplet in the Go Track

Published at Feb 26 2019 · 0 comments
Instructions
Test suite
Solution

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.
``````

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product a * b * c.

## Running the tests

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.

## Further information

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.

## Source

Problem 9 at Project Euler http://projecteuler.net/problem=9

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### pythagorean_triplet_test.go

``````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)
}
}``````
``````package pythagorean

// Triplet is a Pythagorean triplet.
// A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for which, a**2 + b**2 = c**2.
type Triplet [3]int

// Range returns a list of all Pythagorean triplets with sides in the
// range min to max inclusive.
func Range(min int, max int) []Triplet {
result := make([]Triplet, 0)
for a := min; a < max-1; a++ {
for b := a + 1; b < max; b++ {
for c := b + 1; c < max+1; c++ {
if a*a+b*b == c*c {
result = append(result, [3]int{a, b, c})
}
}
}
}

return result
}

// Sum returns a list of all Pythagorean triplets where the sum a+b+c
// (the perimeter) is equal to p.
// a less b less c   therefore   a less sum / 3; b less (sum - 1) / 2
func Sum(sum int) []Triplet {
result := make([]Triplet, 0)
for a := 1; a < sum/3; a++ {
for b := a + 1; b < (sum-1)/2; b++ {
c := sum - a - b
if a*a+b*b == c*c {
result = append(result, [3]int{a, b, c})
}
}
}

return result
}``````