How to check if a number x is power of a number y

Revision en1, by mdarikrayhan, 2022-08-28 11:06:04

I tried to solve the problem 1475A - Нечетный делитель, Where the problem is rather simple we have to check if a number is a power of 2 if true then print NO and if false then YES.It was solved but I thought if I could find if a number x is power of y for long long numbers.So, I wrote the following function:

bool isPowerofN(long long num, long long base)
{
    double log_num = (log(num) / log(base));
    int x = floor(log_num);
    int y = ceil(log_num);
    if (x == y)
        return true;
    else
        return false;
}

The result are correct in my device(mac m1-gcc (Homebrew GCC 11.2.0_3) 11.2.0)

But when submit the code it gives error for larger numbers like 10^40=1099511627776. Here is the submission : 169948113 It should output NO but give YES instead.

So, is there a problem with the implementation or is there another way to check this in constant time ?

Tags number theory, biginteger, powers, recursion

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en6 English mdarikrayhan 2022-08-28 16:39:33 118 Tiny change: ' 2<=base<=$2^{62}$\n' -> ' 2<=base<= $2^{62}$\n'
en5 English mdarikrayhan 2022-08-28 16:11:37 232 Tiny change: ' and in **2^{x+y}e**. Here i' -> ' and in **$2^{x+y}**. Here i'
en4 English mdarikrayhan 2022-08-28 14:50:45 66
en3 English mdarikrayhan 2022-08-28 13:43:31 762 Resolved the issue with function.
en2 English mdarikrayhan 2022-08-28 11:06:48 0 (published)
en1 English mdarikrayhan 2022-08-28 11:06:04 972 Initial revision (saved to drafts)