Logarithm tricks for Competitive Programming | C ++ implementation to check if the number is power of K ; Function to check if the number is power of K ; logarithm function to calculate value ; compare to the result1 or result2 both are equal ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPower ( int N , int K ) { int res1 = log ( N ) / log ( K ) ; double res2 = log ( N ) / log ( K ) ; return ( res1 == res2 ) ; } int main ( ) { int N = 8 ; int K = 2 ; if ( isPower ( N , K ) ) { cout << " Yes " ; } else { cout << " No " ; } return 0 ; }