[LeetCode] Sum of Square Numbers

633. Sum of Square Numbers

Given a non-negative integer c, decide whether there’re two integers a and b such that a2 + b2 = c.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
bool judgeSquareSum(int c) {
long C = c;
for(long a = 0; a * a <= C/2; a++) {
if(C - (a * a) == pow((long)sqrt(C - a * a),2)) {
return true;
}
}
return false;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/02/13/PS/LeetCode/sum-of-square-numbers/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.