Project Euler/62
From charlesreid1
Problem Statement
Cubes that permute to other cubes: find the smallest cube for which exactly 5 permutations of the cube are also cubes.
Link: https://projecteuler.net/problem=62
Solution Technique
We are looking for the smallest cube that permutes with N other cubes.
The key idea here is that, for a particular cube permuting to other cubes, the cubes must all have the same number of digits - which provides a lower and upper bound on the numbers that need to be examined for a particular cube. This means we can break the problems into chunks, each increasing in size by an order of magnitude, and tackle the problem that way. (For reference, the final result is 12 digits!)
The core data structure to do this task quickly is the hash map - we proceed by computing a round of to perfect cubes, and sort their digits. These form the hash map keys, and we store the cube that results in a list so that we can cross-reference it in our search for permuted digits.
Code
The core of the method, where we proceed through the problem in chunks and assemble a hash map to store all of the cubes with N digits:
Map<String,List<BigInteger>> m = new HashMap<String,List<BigInteger>>(); int MAX = 10000; int ceil = 10; while(ceil<MAX) { // Consider another order of magnitude ceil *= 10; for(int p=ceil/10; p<ceil; p++) { BigInteger cube = BigInteger.ONE; // Cube it for(int j=0; j<3; j++) { cube = cube.multiply( BigInteger.valueOf(p) ); } // This is a bit messy. char[] cubeArr = cube.toString().toCharArray(); Arrays.sort(cubeArr); String sortedCubeString = new String(cubeArr); if(m.keySet().contains(sortedCubeString)) { // If this permutation already exists in the map, // add this cube to the list and check if it's the nperms^th case. List<BigInteger> list = m.get(sortedCubeString); list.add(cube); if(list.size()==nperms) { // Someone in list is a winner. return Collections.min(list); } } else { // Start a new list List<BigInteger> list = new ArrayList<BigInteger>(); list.add(cube); m.put(sortedCubeString,list); } } } System.out.println("Search for a cube that permutes "+nperms+" times failed. Search stopped at "+MAX+"."); return null;
Link: https://git.charlesreid1.com/cs/euler/src/master/scratch/Round2_050-070/062/Problem062.java
Flags
Project Euler project euler notes
Round 1: Problems 1-20 Problem 1 · Problem 2 · Problem 3 · Problem 4 · Problem 5 · Problem 6 · Problem 7 · Problem 8 · Problem 9 · Problem 10 Problem 11 · Problem 12 · Problem 13 · Problem 14 · Problem 15 · Problem 16 · Problem 17 · Problem 18 · Problem 19 · Problem 20 Round 2: Problems 50-70 Problem 51 · Problem 52 · Problem 53 · Problem 54 · Problem 55 · Problem 56 · Problem 57 · Problem 58 · ... · Problem 61 · Problem 62 · Problem 63 · Problem 64 · Problem 65 · Problem 66 · Problem 67 Round 3: Problems 100-110 Problem 100 · Problem 101 · Problem 102 Round 4: Problems 500-510 Problem 500 · Problem 501 * · Problem 502 * Round 5: Problems 150-160 Round 6: Problems 250-260 Round 7: Problems 170-180
|