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

Правка en3, от mdarikrayhan, 2022-08-28 13:43:31

Here is the function that can check if a number num is power a anohter number base.

bool isPowerof(long long num, long long base)
{
    long double log_num =log(num) / log(base);
    if (fabs(log_num - round(log_num)) < 0.000001)
    //if difference is less than 10^-5 then they are same number
    {
        return true;
    }
    else
    {
        return false;
    }
}

This function can show accurate results if the number is num<=2^63 and in constant time. Here is a sample submission 169961565.

Теги number theory, biginteger, powers, recursion

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en6 Английский mdarikrayhan 2022-08-28 16:39:33 118 Tiny change: ' 2<=base<=$2^{62}$\n' -> ' 2<=base<= $2^{62}$\n'
en5 Английский 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 Английский mdarikrayhan 2022-08-28 14:50:45 66
en3 Английский mdarikrayhan 2022-08-28 13:43:31 762 Resolved the issue with function.
en2 Английский mdarikrayhan 2022-08-28 11:06:48 0 (published)
en1 Английский mdarikrayhan 2022-08-28 11:06:04 972 Initial revision (saved to drafts)