Given two numbers as strings , find if one is a power of other | Python3 program to check if one number is a power of other ; Multiply the numbers . It multiplies each digit of second string to each digit of first and stores the result . ; If the digit exceeds 9 , add the cumulative carry to previous digit . ; If all zeroes , return "0" . ; Remove starting zeroes . ; Removes Extra zeroes from front of a string . ; Make sure there are no leading zeroes in the string . ; Making sure that s1 is smaller . If it is greater , we recur we reversed parameters . ; Driver Code
def isGreaterThanEqualTo ( s1 , s2 ) : NEW_LINE INDENT if len ( s1 ) > len ( s2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return s1 == s2 NEW_LINE DEDENT def multiply ( s1 , s2 ) : NEW_LINE INDENT n , m = len ( s1 ) , len ( s2 ) NEW_LINE result = [ 0 ] * ( n + m ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( m - 1 , - 1 , - 1 ) : NEW_LINE INDENT result [ i + j + 1 ] += ( int ( s1 [ i ] ) * int ( s2 [ j ] ) ) NEW_LINE DEDENT DEDENT size = len ( result ) NEW_LINE for i in range ( size - 1 , 0 , - 1 ) : NEW_LINE INDENT if result [ i ] >= 10 : NEW_LINE INDENT result [ i - 1 ] += result [ i ] // 10 NEW_LINE result [ i ] = result [ i ] % 10 NEW_LINE DEDENT DEDENT i = 0 NEW_LINE while i < size and result [ i ] == 0 : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if i == size : NEW_LINE INDENT return "0" NEW_LINE DEDENT temp = " " NEW_LINE while i < size : NEW_LINE INDENT temp += str ( result [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT return temp NEW_LINE DEDENT def removeLeadingZeores ( s ) : NEW_LINE INDENT n , i = len ( s ) , 0 NEW_LINE while i < n and s [ i ] == '0' : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if i == n : NEW_LINE INDENT return "0" NEW_LINE DEDENT temp = " " NEW_LINE while i < n : NEW_LINE INDENT temp += s [ i ] NEW_LINE i += 1 NEW_LINE DEDENT return temp NEW_LINE DEDENT def isPower ( s1 , s2 ) : NEW_LINE INDENT s1 = removeLeadingZeores ( s1 ) NEW_LINE s2 = removeLeadingZeores ( s2 ) NEW_LINE if s1 == "0" or s2 == "0" : NEW_LINE INDENT return False NEW_LINE DEDENT if s1 == "1" and s2 == "1" : NEW_LINE INDENT return True NEW_LINE DEDENT if s1 == "1" and s2 == "1" : NEW_LINE INDENT return True NEW_LINE DEDENT if len ( s1 ) > len ( s2 ) : NEW_LINE INDENT return isPower ( s2 , s1 ) NEW_LINE DEDENT temp = s1 NEW_LINE while not isGreaterThanEqualTo ( s1 , s2 ) : NEW_LINE INDENT s1 = multiply ( s1 , temp ) NEW_LINE DEDENT return s1 == s2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 , s2 = "374747" , "52627712618930723" NEW_LINE if isPower ( s1 , s2 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT s1 , s2 = "4099" , "2" NEW_LINE if isPower ( s1 , s2 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT