Smallest string without any multiplication sign that represents the product of two given numbers | 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 ; Product A X B ; Function Call
function len ( A , B ) { let res = " " ; let Log = 0 ; do { Log = Math . floor ( Math . log ( B ) / Math . log ( 2 ) ) ; if ( Log != 0 ) { res = res + String ( A ) + " " + String ( Log ) ; } else { res += A ; break ; } B = B - Math . pow ( 2 , Log ) ; if ( B != 0 ) { res += " " ; } else break ; } while ( Log != 0 ) ; return res ; } function minimumString ( A , B ) { let res1 = len ( A , B ) ; let res2 = len ( B , A ) ; if ( res1 . length > res2 . length ) { document . write ( res2 + " " ) ; } else { document . write ( res1 + " " ) ; } } let A = 6 ; let B = 10 ; minimumString ( A , B ) ;