-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaxicabNumbers4.java
More file actions
33 lines (31 loc) · 1.11 KB
/
TaxicabNumbers4.java
File metadata and controls
33 lines (31 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
import java.util.Hashtable;
public class TaxicabNumbers4 {
public void calculate(long max) {
Hashtable table = new Hashtable<Object, long[]>();
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[] previous = (long[])table.get(ab3);
if(previous == null) {
table.put(ab3, new long[] { a, b });
}
else if(previous[0] != 0) {
System.out.println(ab3 + " = " + a + "³ + " + b + "³ = " + previous[0] + "³ + " + previous[1] + "³");
table.put(ab3, new long[] { 0, 0 });
}
}
}
}
//
public static void main(String[] args) {
TaxicabNumbers4 taxicabNumbers4 = new TaxicabNumbers4();
try {
taxicabNumbers4.calculate(Long.parseLong(args[0]));
}
catch(Exception exception) {
System.err.println("Missing or invalid parameter!");
}
}
}