给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c。
示例1
输入: 5
输出: True
解释: 1 * 1 + 2 * 2 = 5
示例2
输入: 3
输出: False
go
func judgeSquareSum(c int) bool {
a := getIntSqrt(c)
for a >= 0 {
if isSquare(c - a*a) {
return true
}
a--
}
return false
}
func isSquare(c int) bool {
x := getIntSqrt(c)
return x*x == c
}
func getIntSqrt(c int) int {
return int(math.Sqrt(float64(c)))
}