Highest and Smallest power of K less than and greater than equal to N respectively | Python3 implementation of the approach ; 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 ; Function to print the result ; Driver code
import math NEW_LINE def prevPowerofK ( n , k ) : NEW_LINE INDENT p = int ( math . log ( n ) / math . log ( k ) ) NEW_LINE return int ( math . pow ( k , p ) ) NEW_LINE DEDENT def nextPowerOfK ( n , k ) : NEW_LINE INDENT return prevPowerofK ( n , k ) * k NEW_LINE DEDENT def printResult ( n , k ) : NEW_LINE INDENT print ( prevPowerofK ( n , k ) , nextPowerOfK ( n , k ) ) NEW_LINE DEDENT n = 6 NEW_LINE k = 3 NEW_LINE printResult ( n , k ) NEW_LINE