-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.java
61 lines (53 loc) · 1.33 KB
/
Main.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
58
59
60
61
public class Main {
public static void main(String[] args) {
double lower = -10;
double higher = 10;
double possibleResult = 0;
double tolerance = 0.01;
boolean flag = true;
int count = 0;
if (result(0) == 0) {
flag = false;
possibleResult = 0;
}
if (flag) {
if (result(lower) * result(higher) >= 0) {
while (result(lower) * result(higher) >= 0) {
if (result(lower) < 0 && result(higher) < 0) {
higher = higher - tolerance;
lower = lower + tolerance;
count += tolerance;
if (count >= Integer.MAX_VALUE - 100) {
break;
}
}
if (result(lower) > 0 && result(higher) > 0) {
higher = higher - tolerance;
lower = lower + tolerance;
count += tolerance;
if (count >= Integer.MAX_VALUE - 100) {
break;
}
}
}
}
}
if (flag) {
do {
possibleResult = (lower + higher) / 2;
if (result(possibleResult) == 0) {
break;
}
if (result(lower) * result(possibleResult) < 0) {
higher = possibleResult;
} else {
lower = possibleResult;
}
} while (Math.abs(result(possibleResult)) > tolerance);
}
System.out.println(possibleResult);
}
static double result(double x) {
return x * x * Math.sqrt(273 * x) - 233 + 0.31 * x;
}
}