aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-16 21:50:35 -0600
committerjack <[email protected]>2024-06-16 21:50:35 -0600
commit6f7d7ab5663f2880f3b18dc75dfd236f4417f272 (patch)
tree35de5c39e1d4b7b2d22d094a0999ece53d5b1766
parente2d6125e0cfc3e5b0d3f60bb7721cf06dfcc536e (diff)
downloadleetcode-6f7d7ab5663f2880f3b18dc75dfd236f4417f272.tar.gz
leetcode-6f7d7ab5663f2880f3b18dc75dfd236f4417f272.zip
630. sum of square numbers
-rw-r--r--sum_square_numbers.c22
-rw-r--r--sum_square_numbers.py26
2 files changed, 48 insertions, 0 deletions
diff --git a/sum_square_numbers.c b/sum_square_numbers.c
new file mode 100644
index 0000000..f6de14f
--- /dev/null
+++ b/sum_square_numbers.c
@@ -0,0 +1,22 @@
+// 633. Sum of Square Numbers
+#include "leetcode.h"
+
+/*
+ * given a non-negative integer 'c', decide whether there are two integers 'a'
+ * and 'b' such that 'a^2 + b^2 = c'
+ */
+
+bool judgeSquareSum(int c) {
+ int bound = sqrt(c);
+ for (int i = 0; i <= bound; i++) {
+ int a = i * i, b = sqrt(c - a);
+ if (b * b == c - a)
+ return true;
+ }
+ return false;
+}
+
+int main() {
+ printf("%d\n", judgeSquareSum(5)); // expect: 1
+ printf("%d\n", judgeSquareSum(3)); // expect: 0
+}
diff --git a/sum_square_numbers.py b/sum_square_numbers.py
new file mode 100644
index 0000000..2b67d4d
--- /dev/null
+++ b/sum_square_numbers.py
@@ -0,0 +1,26 @@
+# 633. Sum of Square Numbers
+from math import sqrt
+
+"""
+given a non-negative integer 'c', decide whether there are two integers 'a'
+and 'b' such that 'a^2 + b^2 = c'
+"""
+
+
+class Solution(object):
+ def judgeSquareSum(self, c):
+ """
+ :type c: int
+ :rtype: bool
+ """
+ for a in range(int(sqrt(c)) + 1):
+ b = sqrt(c - a * a)
+ if b == int(b):
+ return True
+ return False
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.judgeSquareSum(5)) # expect: true
+ print(obj.judgeSquareSum(3)) # expect: false