From e74d5bfa99a68c37978278ae20eaf5463b7506b5 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 18 Jun 2024 12:10:01 +0200 Subject: [PATCH] =?UTF-8?q?go:=20add=20=C2=AB633.=20Sum=20of=20Square=20Nu?= =?UTF-8?q?mbers=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- go/sum-of-square-numbers.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 go/sum-of-square-numbers.go diff --git a/go/sum-of-square-numbers.go b/go/sum-of-square-numbers.go new file mode 100644 index 0000000..1d1f7ea --- /dev/null +++ b/go/sum-of-square-numbers.go @@ -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 +}