-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSquareRoot(BinarySearch).java
57 lines (53 loc) · 1.92 KB
/
SquareRoot(BinarySearch).java
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
53
54
55
56
57
package hw1;
public class Question1 {
public static int squareRoot(int n)
{
int left = 0;
int right = n;
double middle = (left + right)/2;
int nmiddle = (int) middle;
while (left<=right) {
if (nmiddle * nmiddle < n) {
left = nmiddle + 1;
nmiddle = (left + right)/2;
}
if (nmiddle * nmiddle > n) {
right = nmiddle - 1;
nmiddle = (left + right)/2;
}
if (nmiddle * nmiddle == n) {
return nmiddle;
}
}
return nmiddle;
}
public static void main(String[] args)
{
System.out.println(Question1.squareRoot(1));
System.out.println(Question1.squareRoot(2));
System.out.println(Question1.squareRoot(3));
System.out.println(Question1.squareRoot(4));
System.out.println(Question1.squareRoot(5));
System.out.println(Question1.squareRoot(6));
System.out.println(Question1.squareRoot(7));
System.out.println(Question1.squareRoot(8));
System.out.println(Question1.squareRoot(9));
System.out.println(Question1.squareRoot(10));
System.out.println(Question1.squareRoot(11));
System.out.println(Question1.squareRoot(12));
System.out.println(Question1.squareRoot(13));
System.out.println(Question1.squareRoot(14));
System.out.println(Question1.squareRoot(15));
System.out.println(Question1.squareRoot(16));
System.out.println(Question1.squareRoot(17));
System.out.println(Question1.squareRoot(18));
System.out.println(Question1.squareRoot(19));
System.out.println(Question1.squareRoot(20));
System.out.println(Question1.squareRoot(21));
System.out.println(Question1.squareRoot(22));
System.out.println(Question1.squareRoot(23));
System.out.println(Question1.squareRoot(24));
System.out.println(Question1.squareRoot(25));
System.out.println(Question1.squareRoot(256));
}
}