Advertisement
momo2345

square root

Sep 12th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long int floorSqrt(long long int x);
  5.  
  6.  
  7.  // } Driver Code Ends
  8.  
  9.  
  10. // Function to find square root
  11. // x: element to find square root
  12. long long int floorSqrt(long long int x)
  13. {
  14.     double l=0,r=x,ans;
  15.     const double mx=1e-12;
  16.     for(int i=1;i<=100;i++){
  17.         double mid=(l+r)/2.0;
  18.         //if(abs(mid*mid -x)<=mx) ans= mid;
  19.         if(mid*mid <x) l=mid;
  20.         else r=mid;
  21.     }
  22.     return floor(r);
  23. }
  24.  
  25.  
  26. // { Driver Code Starts.
  27.  
  28. int main()
  29. {
  30.     int t;
  31.     cin>>t;
  32.     while(t--)
  33.     {
  34.         long long n;
  35.         cin>>n;
  36.         cout << floorSqrt(n) << endl;
  37.     }
  38.     return 0;  
  39. }
  40.   // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement