go: add «633. Sum of Square Numbers»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-06-18 12:10:01 +02:00
parent 898040fcad
commit e74d5bfa99
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,28 @@
package main
func judgeSquareSum(c int) bool {
bsearch := func(left, right, target int64) bool {
for left <= right {
mid := left + (right-left)/2
if mid*mid == target {
return true
} else if mid*mid > target {
right = mid - 1
} else {
left = mid + 1
}
}
return false
}
t := int64(c)
for a := int64(0); a*a <= t; a++ {
b := t - a*a
if bsearch(0, b, b) {
return true
}
}
return false
}