4

this is the question: 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.

I know that I can solve it using brute-force method; however, I am wondering if I could solve it in a better way, or can I solve it using recursion technique? Thanks for your kind help!

Chih-Yu Yeh
  • 65
  • 2
  • 9

7 Answers7

4

One approach is to convert to double, and use math to obtain fractional powers of 1/2, 1/3, 1/4, and so on, up to 1/log2 n. The result would be an A; the denominator of the fraction would be P.

Since the computation of the power is in doubles, you would need to try both ceil and floor of the result. Once you hit zero without finding a result, the algorithm could stop.

xenteros
  • 15,586
  • 12
  • 56
  • 91
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

This can also be solved this way.

public boolean isPower(int a) {
    if (a == 1) return true;
    for (int idx = 2; idx * idx <= a; idx ++) {
        double val = Math.log (a)/Math.log (idx);
        if ((val - (int) val) < 0.00000001) return true;
    }
    return false;
}
0
  1. we will check if a == 1 then it can be represented as x ^ 0 hence true . for a > 1 we will check for either 2 or 3 or 4....a; we will divide p (p = a) if p % 2 or ,3 or ,4 or ....... if(p==1) means p is completely divisible by either 2, or 3, or 4 , ...... . means p (where p = a) can we written as x ^ y. hence return true.

 public boolean isPower(int a) {
       if(a==1) return true;
         for (int i = 2; i*i <= a; i++) {
              int p = a;
              while(p%i == 0){
                p/=i;
              }
              if(p == 1) return true;
         }
         return false;

  }
HeadAndTail
  • 804
  • 8
  • 9
0

Code based on @xenteros Answer and a successful submission .

public static int isPower(int A) {
        if(A == 1)
            return 1;
        double Ad = A ;
       for(int i =2;i<=(Math.log(Ad)/Math.log(2));i++)
       {
           double a = Math.pow(Ad,(double)1/i);
           if(Math.ceil(a) == Math.floor(a) || Math.ceil(a)-a <0.000000001)
                return 1;
       }
       return 0;
    }
Shubham Arora
  • 807
  • 9
  • 10
0
while(test--)
{
    int input;
  cin>>input;

  if(input<=2)
  {cout<<"0"<<endl;
  break;}
  //cout<<m;
  int m=sqrt(input);
  int count=2;
  int flag=0;
while(count<=m+1)
{
    if(ceil(log2 (input)/log2 (count))== floor(log2 (input)/log2 (count)))
    {
       // cout<<"ghusa   "<<count<<"  "<<input;
        flag=1;
        cout<<"1";
        break;
    }
    count++;
}
if(flag==0)
{cout<<"0";

}
cout<<endl;  
}

return 0;

}

viman ch das
  • 1
  • 1
  • 1
  • Please describe, what was the problem, and how will this snippet solve it, to help others understand this answer. Additionally, the question is more than 2 years old and has an accepted answer... – FZs Jul 02 '19 at 15:26
0
bool ans(long long int n)
{
    if(n==1)
        return true;
    else
    {
        for (long long int i = 2; i*i <= n; i++)
        {
            if(ceil(log2 (n)/log2 (i)) == floor(log2 (n)/log2 (i)))
            {
                return true;
            }
        }
    }

    return false;
}
Bùi Đức Khánh
  • 3,975
  • 6
  • 27
  • 43
viman ch das
  • 1
  • 1
  • 1
-1

Lets call the initial integer N.

First, you must get all the prime divisors of N.

If N has just 1 divisor, that it is in the form D^k, so it's true.

If it has more than 1 divisor, you should check if the gcd of the number of each divisor is different from 1 and is even.

For example:

12 = 2 * 2 * 3
not possible, GCD(2,1) = 1

24 = 2 * 2 * 2 * 3
not possible, GCD(3,1) = 1

36 = 2 * 2 * 3 * 3
possible,     GCD(2,2) = 2

144 = 2 * 2 * 2 * 2 * 3 * 3
possible,     GCD(4,2) = 2

120 = 2 * 2 * 2 * 3 * 5
not possible, GCD(1,1,3) = 1

216 = 2 * 2 * 2 * 3 * 3 * 3
not possible, GCD(3,3) = 3
Daniel
  • 7,357
  • 7
  • 32
  • 84