3YL5F3GP4G2E2KDK2IJ2BA5S75YEW34P7BAKTNZZHXZRJHT2CNPQC
H3HDA6Q5UBXDO7V5M7HCDKKTCDEQBWOLSMTXMMYGXUQWZDXKJMZAC
DMUTID5VASWBATICSCGHVALEYIBBG2EAIX3WUGHIVOAYGARYYYJQC
MRAK5MHOSG2NF7X2KPREGEPO3DYRAAMU34DH6M55RBERSCVIQPAAC
QIYILOHTZODUXKHKL3EJ5OOENGG744446P7BLNRIK5NDCJHPG7SQC
YLCWVBSCIDWTWW75MAO42TRW2IX5LCN42DWLVVDXTRDFJ4GFXDJAC
WJUYTHBDM6OHRP5UCCM6URFVNJ6XOUP5H6IDQPVCHHCZQUSKCAGQC
NT7TIKSQWJS7N25MYX6O7QKCN6TPHK6ZMZW4NSUIKM5RHWF7OJBAC
5M3GSMBZFQ2X7YJF3SYTOMJLDX3JE6I4TRLTRFWCUXYWWUODY5CQC
package trivial_test
import (
"pythagorean"
"reflect"
"testing"
)
var rangeTests = []struct {
min, max int
ts []pythagorean.Triplet
}{
{1, 10, []pythagorean.Triplet{{3, 4, 5}, {6, 8, 10}}},
{11, 20, []pythagorean.Triplet{{12, 16, 20}}},
}
func TestRange(t *testing.T) {
for _, test := range rangeTests {
ts := trivial.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 []pythagorean.Triplet
}{
{180, []pythagorean.Triplet{{18, 80, 82}, {30, 72, 78}, {45, 60, 75}}},
{1000, []pythagorean.Triplet{{200, 375, 425}}},
}
func TestSum(t *testing.T) {
for _, test := range sumTests {
ts := trivial.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++ {
trivial.Range(1, 100)
}
}
func BenchmarkSum(b *testing.B) {
for i := 0; i < b.N; i++ {
trivial.Sum(1000)
}
}
"pythagorean/trivial"
package euclid_test
import (
"pythagorean"
"pythagorean/euclid"
"reflect"
"testing"
)
var rangeTests = []struct {
min, max int
ts []pythagorean.Triplet
}{
{1, 10, []pythagorean.Triplet{{3, 4, 5}, {6, 8, 10}}},
{11, 20, []pythagorean.Triplet{{12, 16, 20}}},
}
func TestRange(t *testing.T) {
for _, test := range rangeTests {
ts := euclid.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 []pythagorean.Triplet
}{
{180, []pythagorean.Triplet{{18, 80, 82}, {30, 72, 78}, {45, 60, 75}}},
{1000, []pythagorean.Triplet{{200, 375, 425}}},
}
func TestSum(t *testing.T) {
for _, test := range sumTests {
ts := euclid.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++ {
euclid.Range(1, 100)
}
}
func BenchmarkSum(b *testing.B) {
for i := 0; i < b.N; i++ {
euclid.Sum(1000)
}
}
package dickson_test
import (
"pythagorean"
"pythagorean/dickson"
"reflect"
"testing"
)
var rangeTests = []struct {
min, max int
ts []pythagorean.Triplet
}{
{1, 10, []pythagorean.Triplet{{3, 4, 5}, {6, 8, 10}}},
{11, 20, []pythagorean.Triplet{{12, 16, 20}}},
}
func TestRange(t *testing.T) {
for _, test := range rangeTests {
ts := dickson.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 []pythagorean.Triplet
}{
{180, []pythagorean.Triplet{{18, 80, 82}, {30, 72, 78}, {45, 60, 75}}},
{1000, []pythagorean.Triplet{{200, 375, 425}}},
}
func TestSum(t *testing.T) {
for _, test := range sumTests {
ts := dickson.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++ {
dickson.Range(1, 100)
}
}
func BenchmarkSum(b *testing.B) {
for i := 0; i < b.N; i++ {
dickson.Sum(1000)
}
}
// we need the factors of r*r/2: r goes up to max/2, so (max/2)*(max/2)/2 should be enough
// the last 3/4 is just bluffing, I am not aware of hard facts
var factors = genFactors(((MAX / 2) * (MAX / 2)) / 2 * 3 / 4)
for c < max {
for r := 2; r < max/2; r = r + 2 {
fact := factors[(r*r)/2]
fact := factors[(r*r)/2]
for i := 0; i < len(fact)/2; i++ {
s := fact[i]
t := (r * r) / 2 / fact[i]
a = r + s
b = r + t
c = r + s + t
if a >= min && c <= max {
ret = append(ret, pythagorean.Triplet{a, b, c})
}
// r is an _even_ integer, t and s are the factors of r*r/2
for r := 2; r < max/2 && c < max; r = r + 2 {
fact := factors[(r*r)/2]
for i := 0; i < len(fact)/2; i++ {
s := fact[i]
t := (r * r) / 2 / s // r*r = 2*s*t
a = r + s
b = r + t
c = r + s + t
if a >= min && c <= max {
ret = append(ret, pythagorean.Triplet{a, b, c})