Logarithm tricks for Competitive Programming | 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
function isPower ( N , K ) { var res1 = Math . floor ( Math . log ( N ) / Math . log ( K ) ) ; var res2 = Math . log ( N ) / Math . log ( K ) ; return ( res1 == res2 ) ; } var N = 8 ; var K = 2 ; if ( isPower ( N , K ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }