Largest value of x such that axx is N | Python3 implementation of the above approach ; Function to find log_b ( a ) ; Set two pointer 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 pointer ; if number of digits <= n , we can go higher to reach value exactly equal to n ; return the largest value of x ; Driver Code
from math import log10 , ceil , log NEW_LINE def log1 ( a , b ) : NEW_LINE INDENT return log10 ( a ) // log10 ( b ) NEW_LINE DEDENT def get ( a , b , n ) : NEW_LINE INDENT lo = 0 NEW_LINE hi = 1e6 NEW_LINE ans = 0 NEW_LINE while ( lo <= hi ) : NEW_LINE INDENT mid = ( lo + hi ) // 2 NEW_LINE dig = ceil ( ( mid * log ( mid , b ) + log ( a , b ) ) ) NEW_LINE if ( dig > n ) : NEW_LINE INDENT hi = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans = mid NEW_LINE lo = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 2 NEW_LINE b = 2 NEW_LINE n = 6 NEW_LINE print ( int ( get ( a , b , n ) ) ) NEW_LINE DEDENT