Find the smallest number have 3 integer roots?

144 views Asked by At

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);
    }
1

There are 1 answers

1
StackFlowed On BEST ANSWER

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);
}