fork(1) download
  1. public class Main {
  2. public int mySqrt(int x) {
  3. if (x < 2)
  4. return x;
  5. int low = 0, high = x;
  6. while (low < high - 1) {
  7. final int mid = low + high >>> 1;
  8. if (mid <= x / mid) {
  9. low = mid;
  10. } else {
  11. high = mid;
  12. }
  13. }
  14. return low;
  15. }
  16. public static void main(final String[] args){
  17. final Main main = new Main();
  18. final int[] tests = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 24, 25, 26};
  19. for(final int test: tests){
  20. System.out.format("Floor sqrt(%d) = %d%n", test, main.mySqrt(test));
  21. }
  22. }
  23. }
Success #stdin #stdout 0.12s 34068KB
stdin
Standard input is empty
stdout
Floor sqrt(1) = 1
Floor sqrt(2) = 1
Floor sqrt(3) = 1
Floor sqrt(4) = 2
Floor sqrt(5) = 2
Floor sqrt(6) = 2
Floor sqrt(7) = 2
Floor sqrt(8) = 2
Floor sqrt(9) = 3
Floor sqrt(10) = 3
Floor sqrt(24) = 4
Floor sqrt(25) = 5
Floor sqrt(26) = 5