Smallest string without any multiplication sign that represents the product of two given numbers | Java program for the above approach ; Function to find the string which evaluates to the product of A and B ; Stores the result ; 2 ^ log <= B && 2 ^ ( log + 1 ) > B ; Update res to res += A X 2 ^ log ; 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
class GFG { public static String len ( long A , long B ) { String res = " " ; long log = 0 ; do { log = ( long ) ( Math . log ( B ) / Math . log ( 2 ) ) ; if ( log != 0 ) { res += A + " < < " + log ; } else { res += A ; break ; } B = B - ( long ) Math . pow ( 2 , log ) ; if ( B != 0 ) { res += " + " ; } else break ; } while ( log != 0 ) ; return res ; } public static void minimumString ( long A , long B ) { String res1 = len ( A , B ) ; String res2 = len ( B , A ) ; if ( res1 . length ( ) > res2 . length ( ) ) { System . out . println ( res2 ) ; } else { System . out . println ( res1 ) ; } } public static void main ( String args [ ] ) { long A = 6 ; long B = 10 ; minimumString ( A , B ) ; } }