Logarithm tricks for Competitive Programming | Function to return the highest power of k less than or equal to n ; Function to return the smallest power of k greater than or equal to n ; Driver code
function prevPowerofK ( n , k ) { let p = parseInt ( Math . log ( n ) / Math . log ( k ) , 10 ) ; return Math . pow ( k , p ) ; } function nextPowerOfK ( n , k ) { return prevPowerofK ( n , k ) * k ; } let N = 7 ; let K = 2 ; document . write ( prevPowerofK ( N , K ) + " " ) ; document . write ( nextPowerOfK ( N , K ) ) ;