aboutsummaryrefslogtreecommitdiff
path: root/sum_square_numbers.c
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 /sum_square_numbers.c
parente2d6125e0cfc3e5b0d3f60bb7721cf06dfcc536e (diff)
downloadleetcode-6f7d7ab5663f2880f3b18dc75dfd236f4417f272.tar.gz
leetcode-6f7d7ab5663f2880f3b18dc75dfd236f4417f272.zip
630. sum of square numbers
Diffstat (limited to 'sum_square_numbers.c')
-rw-r--r--sum_square_numbers.c22
1 files changed, 22 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
+}