Find the minimum number to be added to N to make it a power of K | Function to return the minimum number to be added to N to make it a power of K . ; Computing the difference between then next greater power of K and N ; Driver code
function minNum ( n , k ) { var x = parseInt ( Math . log ( n ) / Math . log ( k ) ) + 1 ; var mn = Math . pow ( k , x ) - n ; return mn ; } var n = 20 , k = 5 ; document . write ( minNum ( n , k ) ) ;