forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp062.java
More file actions
64 lines (50 loc) · 1.46 KB
/
p062.java
File metadata and controls
64 lines (50 loc) · 1.46 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
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
62
63
64
/*
* Solution to Project Euler problem 62
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public final class p062 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p062().run());
}
public String run() {
int numDigits = 0;
Map<String,Integer> lowest = new HashMap<>();
Map<String,Integer> counts = new HashMap<>();
for (int i = 0; ; i++) {
String numClass = getCubeNumberClass(i);
if (numClass.length() > numDigits) {
// Process and flush data for smaller number of digits
int min = Integer.MAX_VALUE;
for (String nc : counts.keySet()) {
if (counts.get(nc) == 5)
min = Math.min(lowest.get(nc), min);
}
if (min != Integer.MAX_VALUE)
return cube(min).toString();
lowest.clear();
counts.clear();
numDigits = numClass.length();
}
if (!lowest.containsKey(numClass)) {
lowest.put(numClass, i);
counts.put(numClass, 0);
}
counts.put(numClass, counts.get(numClass) + 1);
}
}
private static String getCubeNumberClass(int x) {
char[] digits = cube(x).toString().toCharArray();
Arrays.sort(digits);
return new String(digits);
}
private static BigInteger cube(int x) {
return BigInteger.valueOf(x).pow(3);
}
}