-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqrt.cpp
More file actions
42 lines (37 loc) · 803 Bytes
/
sqrt.cpp
File metadata and controls
42 lines (37 loc) · 803 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
#include <bits/stdc++.h>
using namespace std;
double precision(int n, int sol) {
double factor = 1;
double ans = sol;
for (int i = 0; i < 3; i++) {
factor = factor / 10;
for (double j = ans; j * j < n; j += factor) {
ans = j;
}
}
return ans;
}
int sqrt(int n) {
long long int st = 0, end = n, mid;
double ans = -1;
while (st <= end) {
mid = st + (end - st) / 2;
if (mid * mid == n)
return mid;
else if (mid * mid < n) {
st = mid + 1;
ans = mid;
}
else {
end = mid - 1;
}
}
return ans;
}
int main()
{
int n;
cin >> n;
cout << precision(n, sqrt(n)) << endl;
return 0;
}