Skip to content
Advertisement

java check if digits in square of a number are not in its cube

Define a positive number to be isolated if none of the digits in its square are in its cube. For example 69 is n isolated number because 163*163 = 26569 and 163*163*163 = 4330747 and the square does not contain any of the digits 0, 3, 4 and 7 which are the digits used in the cube. On the other hand 162 is not an isolated number because 162*162=26244 and 162*162*162 = 4251528 and the digits 2 and 4 which appear in the square are also in the cube.

Write a function named isIsolated that returns 1 if its argument is an isolated number, it returns 0 if its not an isolated number and it returns -1 if it cannot determine whether it is isolated or not.

I have tried this:

public static int isIsolated (int n) {
        int square = n*n;
        int cube   = n*n*n;
        int[] a = new int[7];
        int[] b = new int[7];

        if(n < 2097151 || n>1){         
            for(int i=6;i>=0;i--){
                int k = square % 10;
                int l = cube % 10;

                a[i]= k;
                b[j]=l;

                square = square/10; 
                cube = cube/10;
            }

            for(int j=a.length-1;j>=0;j++)
            {
                for(int m=0;m<j;m++)
                {
                    if(a[j] == b[m])
                        return 0;
                    else
                        return 1;
                }
            }
        }
        return -1;
    }

but its not working as expected.

I am a beginner pls help me.

Advertisement

Answer

There were a number of issues with your code.

You were putting 0 in the array once the number was exhausted so any number with less than 7 digits would end up with at least one 0 in teh array and fail your test.

You were leaving the default 0 in the array so any number with a 0 in either it’s square or it’s cube would have gone wrong. This code suggests the highest isolated number whose cube will fit into a long is:

31563 is isolated – ^2 = 996222969 ^3 = 31443785570547

// Max digits.
private static final int DIGITS = 20;
// Indicates an empty digit.
private static final int EMPTY = -1;
// Max value I can handle.
private static final long MAX = (long) Math.cbrt(Long.MAX_VALUE);

public static int isIsolated(long n) {

    if (n > 1 && n < MAX) {
        long square = n * n;
        long cube = n * n * n;
        long[] a = new long[DIGITS];
        Arrays.fill(a, EMPTY);
        long[] b = new long[DIGITS];
        Arrays.fill(b, EMPTY);
        for (int i = 0; i < DIGITS; i++) {
            if (square > 0) {
                a[i] = square % 10;
                square = square / 10;
            }
            if (cube > 0) {
                b[i] = cube % 10;
                cube = cube / 10;
            }
        }

        for (int i = 0; i < DIGITS; i++) {
            if (a[i] != EMPTY) {
                for (int j = 0; j < DIGITS; j++) {
                    if (a[i] == b[j]) {
                        return 0;
                    }
                }
            }
        }
        return 1;
    }
    return -1;
}

public void test(int n) {
    System.out.println(n + " is " + (isIsolated(n) == 1 ? "" : "not ") + "isolated");
}

public void test() {
    System.out.println("Hello");
    test(1234);
    test(69);
    test(162);
    for (long i = 0; i < MAX; i++) {
        if (isIsolated(i) == 1) {
            System.out.println(i + " is isolated - ^2 = " + (i * i) + " ^3 = " + (i * i * i));
        }
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement