Logarithm tricks for Competitive Programming | Python3 implementation to find the previous and next power of K ; 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
from math import log NEW_LINE def prevPowerofK ( n , k ) : NEW_LINE INDENT p = ( int ) ( log ( n ) / log ( k ) ) ; NEW_LINE return pow ( k , p ) ; NEW_LINE DEDENT def nextPowerOfK ( n , k ) : NEW_LINE INDENT return prevPowerofK ( n , k ) * k ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 NEW_LINE K = 2 NEW_LINE print ( prevPowerofK ( N , K ) , end = " ▁ " ) NEW_LINE print ( nextPowerOfK ( N , K ) ) NEW_LINE DEDENT