Largest value of x such that axx is N | Function to find log_b ( a ) ; Set two poleter for binary search ; Calculating number of digits of a * mid ^ mid in base b ; If number of digits > n we can simply ignore it and decrease our poleter ; If number of digits <= n , we can go higher to reach value exactly equal to n ; Return the largest value of x ; Driver Code
function log ( a , b ) { return ( Math . log10 ( a ) / Math . log10 ( b ) ) ; } function get ( a , b , n ) { let lo = 0 , hi = 1e6 ; let ans = 0 ; while ( lo <= hi ) { let mid = Math . floor ( ( lo + hi ) / 2 ) ; let dig = Math . ceil ( ( mid * log ( mid , b ) + log ( a , b ) ) ) ; if ( dig > n ) { hi = mid - 1 ; } else { ans = mid ; lo = mid + 1 ; } } return ans ; } let a = 2 , b = 2 , n = 6 ; document . write ( get ( a , b , n ) + " " ) ;