Fastest and smallest Java O(√ n) solution with all test cases passed


#1
  public class Solution {
  	public int isPower(int a) {
  	if(a==1)return 1;
  	for(int i=2;i<=Math.sqrt(a)+1;i++)
  	{
  	double x=Math.round((Math.pow(a,(1.0/i)))*10000d)/10000d;
  	if(x%1==0)
  	{
  	return 1;
  	}
  	}
  	return 0;
  	}
  	}

“Any number ‘n’ raised to x (where x is from 2 to √ n+1) = if that result is an integer then it can be raised to an integer power”


#2

Without pow or sqrt functions (Code in C++):

int Solution::isPower(int A) {
    if(A == 1) return 1;
    for(int i = 2; i <= INT_MAX / i; i++) {
        int j = i*i;
        while(j < A && j <= INT_MAX / i) j *= i;
        if(j == A) return 1;
    }
    return false;
}