-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
45 lines (40 loc) · 868 Bytes
/
solution.cpp
File metadata and controls
45 lines (40 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// Created by mingyi on 12.04.20.
//
#include <cmath>
#include <cassert>
bool judgeSquareSum(int c) {
long i = 0, j = std::sqrt(c);
while (i <= j) {
long s = i * i + j * j;
if (s == c) return true;
else if (s < c) i++;
else j--;
}
return false;
// slow version
// for (int b = (int) std::sqrt(c / 2); b <= (int) std::sqrt(c); b++) {
// int a2 = c - b * b;
// int a = (int) std::sqrt(a2);
// if (a * a == a2) {
// return true;
// }
// }
// return false;
// slowest version
// for (unsigned a = 0; a * a <= c; a++) {
// double b = std::sqrt(c - a * a);
// if ((unsigned) b == b) {
// return true;
// }
// }
// return false;
}
int main() {
assert(judgeSquareSum(5));
assert(judgeSquareSum(4));
assert(!judgeSquareSum(3));
assert(judgeSquareSum(2));
assert(judgeSquareSum(16));
return 0;
};