Algorithm [InterviewBit]两个整数的幂

Algorithm [InterviewBit]两个整数的幂,algorithm,recursion,Algorithm,Recursion,问题是: 给定一个适合32位有符号整数的正整数,找出它是否可以表示为^P,其中P>1和a>0。A和P都应该是整数 我知道我可以用蛮力的方法解决它;然而,我想知道我是否可以用更好的方法来解决它,或者我可以用递归技术来解决它? 谢谢你的帮助 一种方法是转换为double,并使用数学方法获得1/2、1/3、1/4,等等的分数幂,直到1/log2n。结果将是一个A;分数的分母是P 由于幂的计算是在doubles中进行的,因此您需要同时尝试结果的celi和floor。一旦你在没有找到结果的情况下达到零,算

问题是: 给定一个适合32位有符号整数的正整数,找出它是否可以表示为^P,其中P>1和a>0。A和P都应该是整数

我知道我可以用蛮力的方法解决它;然而,我想知道我是否可以用更好的方法来解决它,或者我可以用递归技术来解决它?
谢谢你的帮助

一种方法是转换为
double
,并使用数学方法获得
1/2
1/3
1/4
,等等的分数幂,直到
1/log2n
。结果将是一个
A
;分数的分母是
P


由于幂的计算是在
double
s中进行的,因此您需要同时尝试结果的
celi
floor
。一旦你在没有找到结果的情况下达到零,算法就会停止。

让我们调用初始整数N

首先,你必须得到N的所有素因子

如果N只有一个除数,那么它的形式是D^k,所以它是真的

如果除数超过1,则应检查每个除数的gcd是否不同于1且为偶数

例如:

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

这也可以这样解决

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;
}
public boolean isPower(int a){
如果(a==1)返回true;
对于(int-idx=2;idx*idx
  • 我们将检查a==1,然后它可以表示为x^0,因此 正确。对于a>1,我们将检查2、3或4……a;我们将 如果p%2或,3或,4或……,则除以p(p=a)。如果(p=1),则表示p为 完全可被2、3或4整除……表示p (其中p=a)我们可以写成x^y,因此返回true

  • public boolean isPower(int a){
    如果(a==1)返回true;
    
    对于(int i=2;i*i代码,基于@xenteros答案和成功提交

    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;
        }
    
    publicstaticintispower(inta){
    如果(A==1)
    返回1;
    双Ad=A;
    对于(int i=2;i
    while)(测试--)
    {
    int输入;
    cin>>输入;
    如果(输入
    bool ans(长整型n)
    {
    如果(n==1)
    返回true;
    其他的
    {
    
    对于(long long int i=2;i*i为什么是“二的幂”?@OliverCharlesworth他的意思是“(两个整数的幂)”@dasblinkenlight:Ohhhh…@Maharaj我最担心的是,如果这个问题可以用递归来解决?你给了他相同的精确解:-)@Xentros无论何时停止,方法都是相同的,如“没有任何区别”。为什么只有0.00000001?您能解释一下方法吗?请简要解释一下您的解决方案。只使用代码的答案不太有用,必须避免。请描述,问题是什么,以及此代码片段将如何解决,以帮助其他人理解此答案。此外,此问题已有两年多的历史,并且有一个公认的答案。。。
    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;
        }
    
    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;
    
    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;
    }