Advertisement
Niloy007

Square root of a number

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