-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathfind_sqrt_using_bs.cpp
51 lines (43 loc) · 1.04 KB
/
find_sqrt_using_bs.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
49
50
51
#include<bits/stdc++.h>
using namespace std;
long long int binarysearch(int n){
int low =0;
int high = n;
long long int mid = low+(high-low)/2;
long long int ans = -1;
while(low<=high){
long long int square = mid*mid;
if(square == n){
return mid;
}
else if(square>n){
high = mid-1;
}
else{
ans = mid;
low = mid+1;
}
mid = low+(high-low)/2;
}
return ans;
}
double moreprecision(int n,int precision,int tempsol){
double factor = 1;
double ans = tempsol;
for(int i = 0;i<precision;i++){
factor = factor/10;
for(double j = ans;j*j < n;j = j+factor){
ans = j;
}
}
return ans;
}
int main()
{
int n;
cin>>n;
int tempsol = binarysearch(n);
// cout<<tempsol<<setprecision(4)<<endl;
cout<<moreprecision(n,4,tempsol)<<endl;
return 0;
}