Check if one integer is an integer power of another

后端 未结 13 1574
臣服心动
臣服心动 2020-11-27 05:15

This is an interview question: \"Given 2 integers x and y, check if x is an integer power of y\" (e.g. for x = 8 and y = 2 the answer is \"true\", and for x = 10 and y = 2 \

13条回答
  •  醉话见心
    2020-11-27 05:28

    I found this Solution //Check for If A can be expressed as power of two integers

        int isPower(int A)
        {
         int i,a;
         double p;
         if(A==1)
         return 1;
         for(int a=1; a<=sqrt(A);++a )
         {
             p=log(A)/log(a);
             if(p-int(p)<0.000000001)
             return 1;
         }
        return 0;
       }
    

    binarycoder.org

提交回复
热议问题