// 0009.go contains some functions to solve ProjectEuler.net's Problem #9
// https://projecteuler.net/problem=9
package main
import (
"fmt"
"math"
)
// Pythagorean triplet returns the pythagorean a, b, c given the sum of a, b, c.
// If the combination does not exist, the result is -1, -1, -1
func PythagoreanTriplet(sum int) (int, int, int) {
// Initialise variables
var a2b2, c float64
for i := 1; i <= sum; i++ {
for j := 1; j <= sum; j++ {
a2b2 = float64(i*i + j*j)
c = math.Sqrt(a2b2)
if c == math.Trunc(c) && i+j+int(c) == sum {
// Combination found, return results
return i,j,int(c)
}
}
}
// Nothing found
return -1, -1, -1
}
func main(){
// Get result
a,b,c := PythagoreanTriplet(1000)
fmt.Println(a*b*c)
}
Comments
0 B
|👍
/👎
0 B
|👍
/👎