-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrt.java
More file actions
52 lines (44 loc) · 1.11 KB
/
Sqrt.java
File metadata and controls
52 lines (44 loc) · 1.11 KB
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
48
49
50
51
52
package leetcode;
import org.junit.Test;
import junit.framework.TestCase;
/**
* Link: https://leetcode.com/problems/sqrtx/
*
* @author shivam.maharshi
*/
public class Sqrt extends TestCase {
@Test
public static void test() {
assertEquals(0, mySqrt(0));
assertEquals(1, mySqrt(1));
assertEquals(1, mySqrt(2));
assertEquals(2, mySqrt(4));
assertEquals(2, mySqrt(8));
assertEquals(5, mySqrt(25));
assertEquals((int) Math.sqrt(Integer.MAX_VALUE),
mySqrt(Integer.MAX_VALUE));
assertEquals((int) Math.sqrt(2147395599), mySqrt(2147395599));
}
// TODO: This is another approach. Make it work.
public static int ms(int N) {
int x, j;
x = 0;
for (j = 1 << 31; j != 0; j >>= 1) {
x = x + j;
if (x * x > N)
x = x - j;
}
return x;
}
public static int mySqrt(int x) {
if (x < 1)
return 0;
double a = x, b = 1;
// Tests will fail if not precise results.
while (a - b > 0.0000000000000001) {
a = b + (a - b) / 2;
b = x / a;
}
return (int) a;
}
}