Post

633 Sum Of Square Numbers

633 Sum Of Square Numbers

Sum of Square Numbers image

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

 

Example 1:

1
2
3
4
5
**Input:** c = 5
**Output:** true
**Explanation:** 1 * 1 + 2 * 2 = 5

Example 2:

1
2
3
4
**Input:** c = 3
**Output:** false

 

Constraints:

1
0 <= c <= 231 - 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

bool judgeSquareSum(int c) {
    if (c == 0) {
        return true;
    }
    for (int i = 1 ; i <= (int)(sqrt(c)) ; i ++) {
        double k = sqrt(c- i*i);
        if ((int)(k) == k) {
            //printf(" %d %d\n",k, i);
            return true;
        }
    }
    return false;
    
}



This post is licensed under CC BY 4.0 by the author.