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:

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement