Smallest string without any multiplication sign that represents the product of two given numbers | Python3 program for the above approach ; Function to find the string which evaluates to the product of A and B ; Stores the result ; 2 ^ logg <= B && 2 ^ ( logg + 1 ) > B ; Update res to res += A X 2 ^ logg ; Update res to res += A X 2 ^ 0 ; Find the remainder ; If remainder is not equal to 0 ; Return the resultant string ; Function to find the minimum length representation of A * B ; Find representation of form A << k1 + A << k2 + ... + A << kn ; Find representation of form B << k1 + B << k2 + ... + B << kn ; Compare the length of the representations ; Driver Code ; Product A X B ; Function call
from math import log NEW_LINE def lenn ( A , B ) : NEW_LINE INDENT res = " " NEW_LINE logg = 0 NEW_LINE while True : NEW_LINE INDENT logg = log ( B ) // log ( 2 ) NEW_LINE if ( logg != 0 ) : NEW_LINE INDENT res += ( str ( A ) + " < < " + str ( int ( logg ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT res += A NEW_LINE break NEW_LINE DEDENT B = B - pow ( 2 , logg ) NEW_LINE if ( B != 0 ) : NEW_LINE INDENT res += " + " NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT if logg == 0 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def minimumString ( A , B ) : NEW_LINE INDENT res1 = lenn ( A , B ) NEW_LINE res2 = lenn ( B , A ) NEW_LINE if ( len ( res1 ) > len ( res2 ) ) : NEW_LINE INDENT print ( res2 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( res1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 6 NEW_LINE B = 10 NEW_LINE minimumString ( A , B ) NEW_LINE DEDENT