-4

java question

Find the smallest number x, such that x > 1, and Square Roots , Cube root and Fifth Roots are all integers??

i tried this code in java, but no result ?

    int i = 1;
    while (true) {
        i++;
        if (Math.pow(i, 1.0 / 2) % 1 == 0 &&
                Math.pow(i, 1.0 / 3) % 1 == 0 &&
                Math.pow(i, 1.0 / 5) % 1 == 0) {
            break;
        }
        System.out.println(i);
    }
  • 6
    That is more a math than a java question... And if you spend more than 5 minutes thinking about it you should be able to come up with something... – assylias Nov 14 '14 at 19:49
  • This question appears to be off-topic because it is about doing someone else homework. – But I'm Not A Wrapper Class Nov 14 '14 at 19:53
  • Tell us what you have tried to do, and we'll help you finish. – boisvert Nov 14 '14 at 19:55
  • What do you mean by no result? What are you getting and what do you expect? – But I'm Not A Wrapper Class Nov 14 '14 at 20:08
  • 2
    Using `% 1 == 0` to test whether a floating-point number is an integer is very unreliable. This is especially the case when using `Math.pow` to the 1.0/3 or 1.0/5 power, since those fractions can't be represented exactly. I tested it using 1.0/3, and it returns `true` for 1, 8, and 27, but nothing higher. – ajb Nov 14 '14 at 20:11

1 Answers1

0

Your if condition is not correct !

Your code should be :

public static void main(String [] args){
    BigInteger i = new BigInteger("2");
    double sqroot, cuberoot, fifthroot;
    while(true) {
        sqroot = Math.sqrt(i.floatValue());
        cuberoot = Math.cbrt(i.floatValue());
        fifthroot = Math.pow(i.floatValue(),1/5.0d);
        System.out.print("i = "+i);
        if(Math.floor(sqroot)==sqroot && Math.floor(cuberoot)==cuberoot && Math.floor(fifthroot)==fifthroot){
             break;
        }
        i= i.add(new BigInteger("1"));
    }
    System.out.println(i);
}
StackFlowed
  • 6,664
  • 1
  • 29
  • 45