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
}
