-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoot.java
More file actions
35 lines (25 loc) · 741 Bytes
/
Copy pathRoot.java
File metadata and controls
35 lines (25 loc) · 741 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.*;
public class Root {
public static int mySqrt(int x) {
if (x == 0 || x == 1) return x;
int low = 1, high = x, ans = 0;
while (low <= high) {
int mid = low + (high - low) / 2;
if (mid <= x / mid) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int x = sc.nextInt();
int result = mySqrt(x);
System.out.println("Square root (floor) = " + result);
sc.close();
}
}