Sunday, 24 December 2017

Question :
Define a positive number to be isolated if none of the digits in its square are in its cube. For example 163 is n isolated number because 69*69 =  26569  and 69*69*69 = 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 (see the note below). The function signature is:

int isIsolated(long n)

Note that the type of the input parameter is long. The maximum positive number that can be represented as a long is 63 bits long. This allows us to test numbers up to 2,097,151 because the cube of 2,097,151 can be represented as a long. However, the cube of 2,097,152 requires more than 63 bits to represent it and hence cannot be computed without extra effort. Therefore, your function should test if n is larger than 2,097,151 and return -1 if it is. If  n is less than 1 your function should also return -1.

Hint: n % 10 is the rightmost digit of n, n = n/10 shifts the digits of n one place to the right.

The first 10 isolated numbers are

N
n*n
n*n*n
2
4
8
3
9
27
8
64
512
9
81
729
14
196
2744
24
576
13824
28
784
21952
34
1156
39304
58
3364
195112
63
3969
250047
 public static int isIsolated(long n)
        {
            long square = 0, cube = 0, rem = 0, remSquare = 0, temSquare = 0;
            int rtnVal = 1;
            if (n <= 2097151)
            {
                square = n * n;
                cube = n * n * n;

                while (cube != 0)
                {
                    temSquare = square;
                    rem = cube % 10;
                    cube = cube / 10;
                    while (temSquare != 0)
                    {
                        remSquare = temSquare % 10;
                        temSquare = temSquare / 10;
                        if (rem == remSquare)
                        {
                            rtnVal = 0;
                            goto label;

                        }
                    }
                }
            }
            else
            {
                rtnVal = 0;
            }
        label:
            return rtnVal;
        }

2 comments:

  1. Wouldnt it be nice if we return -1 if this condition if (n <= 2097151) is false

    ReplyDelete
  2. My C version of the solution


    #include
    int isIsolated(long n);
    int main(void) {
    printf("%i\n",isIsolated(2097152));
    return 0;
    }

    int isIsolated(long n){
    if (n > 2097151) return -1;
    long square=n*n, cube =n*n*n, current_cube=0;
    int remainder_of_square=0, remainder_of_cube=0;
    while(square!=0){
    current_cube=cube;
    remainder_of_square=square%10;
    square=square/10;
    while(current_cube!=0){
    remainder_of_cube=current_cube%10;
    current_cube = current_cube/10;
    if(remainder_of_cube==remainder_of_square) return 0;
    }
    }
    return 1;
    }

    ReplyDelete

Powered by Blogger.

Followers

Translate

Currency Converter

Exchange Rate

Featured post

Interpolation in angular 5

When we want to communicate from component class to template ,we can use interpolation which use the { { expression }} to render the bound...

Popular Posts

My Facebook Page