-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSquareRoot.cpp
48 lines (40 loc) · 1001 Bytes
/
SquareRoot.cpp
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
46
47
48
// compute and return the square root of x
// Leetcode https://leetcode.com/explore/interview/card/top-interview-questions-medium/113/math/819/
// Techie delight https://www.techiedelight.com/find-square-root-using-binary-search-algorithm/
#include <iostream>
#include <climits>
#include <vector>
int mySqrt(const int x)
{
if (x <= 1) {
return x;
}
int low = 1;
int high = x / 2;
int result;
while (low <= high) {
int mid = low + (high - low) / 2;
long square = mid * mid;
if (square == x) {
return mid;
} else if (square < x) {
low = mid + 1;
result = mid;
} else {
high = mid - 1;
}
}
return result;
}
void test(const int x)
{
std::cout << "Sqaure root of " << x << " : " << mySqrt(x) << "\n";
}
int main()
{
std::vector<int> xvalue {0, 1, 2, 3, 4, 5, 8, 23, 1000, 1024, INT_MAX};
for (auto x : xvalue) {
test(x);
}
return 0;
}