Power Of Two Integers

Given a positive integer which fits in a 32 bit signed integer, find if it can be expressed as A^P where P > 1 and A > 0. A and P both should be integers.

Example

Input : 4
Output : True  
as 2^2 = 4. 
public class Solution {
    public int isPower(int A) {
        if (A == 1)
            return 1;
        if (A <= 3)
            return 0;
        for (int i = 2; i * i <= A; i++) {
            int pow = (int) (Math.log(A) / Math.log(i));
            if (Math.pow(i, pow) == A)
                return 1;
        }
        return 0;
    }
}

Last updated