-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaxicabNumbers2.java
29 lines (28 loc) · 1013 Bytes
/
TaxicabNumbers2.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
public class TaxicabNumbers2 {
public void calculate(long max) {
long a3 = 0;
for(long a = 1; (a3 = a * a * a) < max; a++) {
long b3 = 0;
for(long b = a; (b3 = b * b * b) <= max - a3; b++) {
long ab3 = a3 + b3;
long c3 = 0;
for(long c = a + 1; (c3 = c * c * c) < ab3; c++) {
double d3 = ab3 - c3;
long d = (long)Math.cbrt(d3);
if(d >= c && d != a && d != b && d3 == d * d * d)
System.out.println(ab3 + " = " + a + "³ + " + b + "³ = " + c + "³ + " + d + "³");
}
}
}
}
//
public static void main(String[] args) {
TaxicabNumbers2 taxicabNumbers2 = new TaxicabNumbers2();
try {
taxicabNumbers2.calculate(Long.parseLong(args[0]));
}
catch(Exception exception) {
System.err.println("Missing or invalid parameter!");
}
}
}