-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCubicPermutations.java
More file actions
35 lines (28 loc) · 1.12 KB
/
CubicPermutations.java
File metadata and controls
35 lines (28 loc) · 1.12 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
package dev;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class CubePermutations {
public static void main(String[] args) {
Map<String, Integer> cubeCounts = new HashMap<>();
Map<String, Long> cubeValues = new HashMap<>();
int n = 1;
while (true) {
long cube = (long) n * n * n;
char[] digits = Long.toString(cube).toCharArray();
Arrays.sort(digits);
String sortedDigits = new String(digits);
// Count the number of times this permutation of digits has been seen
cubeCounts.put(sortedDigits, cubeCounts.getOrDefault(sortedDigits, 0) + 1);
// Store the smallest cube for this set of digits
if (!cubeValues.containsKey(sortedDigits)) {
cubeValues.put(sortedDigits, cube);
}
if (cubeCounts.get(sortedDigits) == 5) {
System.out.println("The smallest cube for which exactly five permutations of its digits are cube is: " + cubeValues.get(sortedDigits));
break;
}
n++;
}
}
}