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