-
-
Notifications
You must be signed in to change notification settings - Fork 657
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from exercism/pythagorean-triplet
add exercise pythagorean triplet
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package pythagorean | ||
|
||
type Triplet [3]int | ||
|
||
func pyth(a, b, c int) bool { | ||
return a*a+b*b == c*c | ||
} | ||
|
||
func Range(min, max int) (ts []Triplet) { | ||
for a := min; a <= max; a++ { | ||
for b := a; b <= max; b++ { | ||
for c := b; c <= max; c++ { | ||
if pyth(a, b, c) { | ||
ts = append(ts, Triplet{a, b, c}) | ||
} | ||
} | ||
} | ||
} | ||
return | ||
} | ||
|
||
func Sum(sum int) (ts []Triplet) { | ||
max := sum / 2 | ||
for a := 1; a <= max; a++ { | ||
for b := a; b <= max; b++ { | ||
if c := sum - a - b; pyth(a, b, c) { | ||
ts = append(ts, Triplet{a, b, c}) | ||
} | ||
} | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package pythagorean | ||
|
||
// Use this type defintion, | ||
// | ||
// 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) | ||
} | ||
} |