Finding the smallest cube for which exactly five permutations of its digits are cube (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 &lt; 999; i++) {
        if (!checked[i]) {              
            int cube_perms = 0;
            long i_3 = i * i * i;
            int len = String.valueOf(i_3).length();
            ArrayList&lt;int[]&gt; perms = getPermutations(longToArray(i_3));
            for (int[] x : perms) {
                int num = arrayToInt(x);
                if (String.valueOf(num).length() &lt; 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&lt;int[]&gt; getPermutations(int[] arr) {
    ArrayList&lt;int[]&gt; al = new ArrayList&lt;int[]&gt;();
    Arrays.sort(arr);
    do {
        al.add(arr.clone());
    } while (getNextPermDistinct(arr));
    return al;
}


getNextPermDistinct()

public static boolean getNextPermDistinct(int[] array) {
    // Find longest non-increasing suffix
    int i = array.length - 1;
    while (i &gt; 0 &amp;&amp; array[i - 1] &gt;= array[i])
        i--;
    // Now i is the head index of the suffix

    // Are we at the last permutation already?
    if (i &lt;= 0)
        return false;

    // Let array[i - 1] be the pivot
    // Find rightmost element that exceeds the pivot
    int j = array.length - 1;
    while (array[j] &lt;= array[i - 1])
        j--;
    // Now the value array[j] will become the new pivot
    // Assertion: j &gt;= i

    // Swap the pivot with j
    int temp = array[i - 1];
    array[i - 1] = array[j];
    array[j] = temp;

    // Reverse the suffix
    j = array.length - 1;
    while (i &lt; j) {
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
        i++;
        j--;
    }

    // Successfully computed the next permutation
    return true;
}


longToArray()

public static int[] longToArray(long i) {
    int len = String.valueOf(i).length();
    int[] arr = new int[len];
    for (int j = 0; j &lt; len; j++) {
        arr[j] = (int)i % 10;
        i /= 10;
    }
    return arr;
}


arrayToInt()

public static int arrayToInt(int[] a) {
    int num = 0;
    for (int i = a.length - 1; i &gt;= 0; i--) {
        num += a[i] * Math.pow(10, a.length - i - 1);
    }
    return num;
}
Change this code:


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



To this:

boolean[] checked = new boolean[999];
for (int i = 1; i &lt; 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&lt;int[]&gt; perms =, use List&lt;int[]&gt; perms =
The getPermutations method returns List&lt;int[]&gt;, 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&lt;Integer&gt; where the value are already converted into a form that you actually need.