Maximum number with same digit factorial product | Function to return the required number ; Count the frequency of each digit ; 4 ! can be expressed as 2 ! * 2 ! * 3 ! ; 6 ! can be expressed as 5 ! * 3 ! ; 8 ! can be expressed as 7 ! * 2 ! * 2 ! * 2 ! ; 9 ! can be expressed as 7 ! * 3 ! * 3 ! * 2 ! ; To store the required number ; If number has only either 1 and 0 as its digits ; Generate the greatest number possible ; Driver code
function getNumber ( s ) { let number_of_digits = s . length ; let freq = new Array ( 10 ) ; freq . fill ( 0 ) ; for ( let i = 0 ; i < number_of_digits ; i ++ ) { if ( s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' s [ i ] == ' ' ) { freq [ s [ i ] . charCodeAt ( ) - 48 ] += 1 ; } if ( s [ i ] == ' ' ) { freq [ 2 ] += 2 ; freq [ 3 ] ++ ; } if ( s [ i ] == ' ' ) { freq [ 5 ] ++ ; freq [ 3 ] ++ ; } if ( s [ i ] == ' ' ) { freq [ 7 ] ++ ; freq [ 2 ] += 3 ; } if ( s [ i ] == ' ' ) { freq [ 7 ] ++ ; freq [ 3 ] += 2 ; freq [ 2 ] ++ ; } } let t = " " ; if ( freq [ 1 ] == number_of_digits || freq [ 0 ] == number_of_digits || ( freq [ 0 ] + freq [ 1 ] ) == number_of_digits ) { return s ; } else { for ( let i = 9 ; i >= 2 ; i -- ) { let ctr = freq [ i ] ; while ( ( ctr -- ) > 0 ) { t += String . fromCharCode ( i + 48 ) ; } } return t ; } } let s = " " ; document . write ( getNumber ( s ) ) ;