Create-sqrt-monotonic-binary-search.cpp (#114)
* Create sqrt_monotonic_binary_search.cpp * Rename sqrt_monotonic_binary_search.cpp to sqrt-monotonic-binary-search.cpp * update string index readme add sqrt-monotonic-binary-search.cpp under c-or-cpp Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>pull/131/head
parent
e133ec61bd
commit
6cd32d216b
|
@ -5,7 +5,8 @@
|
|||
1. [Linear Search](c-or-cpp/linear-search.cpp)
|
||||
2. [Binary Search](c-or-cpp/binary-search.cpp)
|
||||
3. [Jump Search](c-or-cpp/jump-search.cpp)
|
||||
4. [Interpolation Search](c-or-cpp/interpolation-search.cpp)
|
||||
4. [finding squareroot using binary search](c-or-cpp/sqrt-monotonic-binary-search.cpp)
|
||||
5. [Interpolation Search](c-or-cpp/interpolation-search.cpp)
|
||||
|
||||
### Python
|
||||
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
#include <iostream>
|
||||
using namespace std;
|
||||
float square_root(int a,int p){
|
||||
int s=0;
|
||||
int e=a;
|
||||
float ans=-1;
|
||||
int mid;
|
||||
while(s<=e){
|
||||
mid=(s+e)/2;
|
||||
if (mid*mid==a)
|
||||
return mid;
|
||||
else if(mid*mid<a){
|
||||
s=mid+1;
|
||||
ans=mid;
|
||||
}
|
||||
else
|
||||
e=mid-1;
|
||||
}
|
||||
float inc=0.1;
|
||||
for(int times=1;times<=p;times++){
|
||||
while(ans*ans<=a){
|
||||
ans=ans+inc;
|
||||
}
|
||||
ans=ans-inc;
|
||||
inc=inc/10;
|
||||
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
cin>>n;
|
||||
|
||||
cout<<square_root(n,3);
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue