Check if any permutation of N equals any power of K | function to check if N and K are anagrams ; Function to check if any permutation of N exist such that it is some power of K ; generate all power of K under 10 ^ 18 ; check if any power of K is valid ; Driver Code ; function call to print required answer
def isValid ( N , K ) : NEW_LINE INDENT m1 = [ ] NEW_LINE m2 = [ ] NEW_LINE while ( N > 0 ) : NEW_LINE INDENT m1 . append ( N % 10 ) NEW_LINE N //= 10 NEW_LINE DEDENT while ( K > 0 ) : NEW_LINE INDENT m2 . append ( K % 10 ) NEW_LINE K //= 10 NEW_LINE DEDENT if ( m1 == m2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def anyPermutation ( N , K ) : NEW_LINE INDENT powK = [ 0 ] * 100 NEW_LINE Limit = pow ( 10 , 18 ) NEW_LINE powK [ 0 ] = K NEW_LINE i = 1 NEW_LINE while ( powK [ i - 1 ] * K < Limit ) : NEW_LINE INDENT powK [ i ] = powK [ i - 1 ] * K NEW_LINE i += 1 NEW_LINE DEDENT for j in range ( i ) : NEW_LINE INDENT if ( isValid ( N , powK [ j ] ) ) : NEW_LINE INDENT return " True " NEW_LINE DEDENT DEDENT return " False " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 96889010407 NEW_LINE K = 7 NEW_LINE print ( anyPermutation ( N , K ) ) NEW_LINE DEDENT