-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquareRoot.java
More file actions
32 lines (31 loc) · 751 Bytes
/
SquareRoot.java
File metadata and controls
32 lines (31 loc) · 751 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
package Searching;
/**
* @author Vishal Singh */
public class SquareRoot {
static long findSquareRoot(long num){
if (num == 0 || num == 1){
return num;
}
long start = 1;
long end = num;
long ans = 0;
while (start <= end){
long mid = (start + end)/2;
if (mid*mid == num) {
return mid;
}
if (mid*mid < num){
start = mid+1;
ans = mid;
}
else {
end = mid-1;
}
}
return ans;
}
public static void main(String[] args) {
long rootOf = 17054520;
System.out.println(findSquareRoot(rootOf));
}
}