fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int mySqrt(int x) {
  5. int low = 0, high = x;
  6. float mid = 0;
  7.  
  8. while (low <= high) {
  9. mid = low + (high-low)/2;
  10. if (mid == (x/mid))
  11. return int(mid);
  12. else if (x > mid) low = mid+1;
  13. else high = mid-1;
  14. }
  15. return low;
  16. }
  17. int main() {
  18. cout << mySqrt(8) << " " << mySqrt(4)<< " ";
  19. return 0;
  20. }
  21.  
Success #stdin #stdout 0s 5436KB
stdin
Standard input is empty
stdout
8 2