-
Notifications
You must be signed in to change notification settings - Fork 20
/
SqrtX.kt
executable file
·47 lines (42 loc) · 1005 Bytes
/
SqrtX.kt
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
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Implement int sqrt(int x).
* Compute and return the square root of x.
*
* Accepted.
*/
class SqrtX {
fun mySqrt(x: Int): Int {
// A tricky way to solve the problem.
// return Math.sqrt(x.toDouble()).toInt()
if (x <= 0) {
return 0
}
if (x <= 3) {
return 1
}
var high = x / 2
var low = 1
// To avoid overflow.
if (x >= 46340 * 46340) {
return 46340
}
if (high > 46340) {
high = 46340
}
var mid = (high + 1) / 2
do {
when {
mid * mid > x -> high = mid - 1
mid * mid < x -> {
if ((mid + 1) * (mid + 1) > x) {
return mid
}
low = mid + 1
}
else -> return mid
}
mid = (low + high) / 2
} while (high > low)
return mid
}
}