Advertisement
sajid161

19:2

May 15th, 2021
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. //integer bisection.
  2. class Solution {
  3.   public:
  4.     int countSquares(int N) {
  5.         N--;
  6.         long long l=0,r=N,ans;
  7.          while(l<=r)
  8.          {
  9.            if(ans*ans>N) break;
  10.           long long mid=(l+r)/2;
  11.           if(mid*mid>N) r=mid-1;
  12.           else
  13.           {
  14.  
  15.             ans=mid;
  16.             l=mid+1;
  17.            }
  18.          }
  19.  
  20.         return ans;
  21.     }
  22. };
  23. //fractional bisection
  24. #include<bits/stdc++.h>
  25. using namespace std;
  26. int main()
  27. {
  28.     long long int x;
  29.     cin>>x;
  30.     x--;
  31.     double l=0,r=x;
  32.  
  33.     for(int i=0;i<100;i++)
  34.     {
  35.        double mid=(l+r)/2.0;
  36.         if(mid*mid>x) r=mid;
  37.         else l=mid;
  38.     }
  39.     cout<<floor(l);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement