HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Finding the smallest cube for which exactly five permutations of its digits are cube

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
smallestthefivearedigitsitsforcubefindingwhich

Problem

(Project Euler #62)


The digits of the cube, 41063625 (3453), can be permuted to produce
two other cubes:


56623104 (3843) and 66430125 (4053). In fact, 41063625 is the
smallest cube which has exactly three permutations of its digits which
are also cube.


Find the smallest cube for which exactly five permutations of its
digits are cube.

Here is my code which properly works and prints the number of permutations which are cube for a particular i. I have found numbers which have 1,2,3 permutations as perfect cubes. Here is the output which checked from i=0 to i=999 in around 120~130 seconds and found no such number. I need to increase the efficiency of the code otherwise searching will take a very long time. With increase in i the number of digits in cube of i increases and total permutations increase rapidly, hence a rapid decrease in speed.

It also prints 345,3 (correct) and none of 384 or 405 (good).

boolean[] checked = new boolean[999];
    Arrays.fill(checked, false);
    for (int i = 1; i  perms = getPermutations(longToArray(i_3));
            for (int[] x : perms) {
                int num = arrayToInt(x);
                if (String.valueOf(num).length() < len) {
                    continue;
                }
                int cubrt = (int) Math.round(Math.pow(num, 1.0 / 3.0));
                if (Math.pow(cubrt, 3) == (num)) {
                    checked[cubrt] = true;
                    ++cube_perms;
                }
            }
            if (cube_perms == 5) {
                return i;
            }

            System.out.println(i + "," + cube_perms);
        }
    }
    return 0;


getPermutations()

public static ArrayList getPermutations(int[] arr) {
    ArrayList al = new ArrayList();
    Arrays.sort(arr);
    do {
        al.add(arr.clone());
    } while (getNextPermDistinct(arr));
    return al;
}


getNextPermDistinct()

```
public static boolean getNextPermDistinct(int[] array) {
/

Solution

Change this code:

boolean[] checked = new boolean[999];
Arrays.fill(checked, false);
for (int i = 1; i < 999; i++) {
    if (!checked[i]) {


To this:

boolean[] checked = new boolean[999];
for (int i = 1; i < checked.length; i++) {
    if (!checked[i]) {


The benefits:

  • boolean[] has all false values by default, no need to fill



  • Instead of iterating until some number, iterate until the something with a name that is required for the loop to work correctly, in this case checked.length



Other minor things:

  • The convention for naming variables is camelCase instead of snake_case



  • Refer to types by interfaces. Instead of ArrayList perms =, use List perms =



  • The getPermutations method returns List, but you never actually use the int[] values, you immediately convert them to an int using arrayToInt. It seems it would be better if the method returned List where the value are already converted into a form that you actually need.

Code Snippets

boolean[] checked = new boolean[999];
Arrays.fill(checked, false);
for (int i = 1; i < 999; i++) {
    if (!checked[i]) {
boolean[] checked = new boolean[999];
for (int i = 1; i < checked.length; i++) {
    if (!checked[i]) {

Context

StackExchange Code Review Q#92634, answer score: 6

Revisions (0)

No revisions yet.