Logarithm tricks for Competitive Programming | Java 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
class GFG { static boolean isPower ( int N , int K ) { int res1 = ( int ) ( Math . log ( N ) / Math . log ( K ) ) ; double res2 = Math . log ( N ) / Math . log ( K ) ; return ( res1 == res2 ) ; } public static void main ( String [ ] args ) { int N = 8 ; int K = 2 ; if ( isPower ( N , K ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }