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
< ? php function getNumber ( $ s ) { $ number_of_digits = strlen ( $ s ) ; $ freq = array_fill ( 0 , 10 , 0 ) ; for ( $ i = 0 ; $ i < $ number_of_digits ; $ i ++ ) { if ( $ s [ $ i ] == '1' $ s [ $ i ] == '2' $ s [ $ i ] == '3' $ s [ $ i ] == '5' $ s [ $ i ] == '7' ) { $ freq [ ord ( $ s [ $ i ] ) - 48 ] += 1 ; } if ( $ s [ $ i ] == '4' ) { $ freq [ 2 ] += 2 ; $ freq [ 3 ] ++ ; } if ( $ s [ $ i ] == '6' ) { $ freq [ 5 ] ++ ; $ freq [ 3 ] ++ ; } if ( $ s [ $ i ] == '8' ) { $ freq [ 7 ] ++ ; $ freq [ 2 ] += 3 ; } if ( $ s [ $ i ] == '9' ) { $ freq [ 7 ] ++ ; $ freq [ 3 ] += 2 ; $ freq [ 2 ] ++ ; } } $ t = " " ; if ( $ freq [ 1 ] == $ number_of_digits || $ freq [ 0 ] == $ number_of_digits || ( $ freq [ 0 ] + $ freq [ 1 ] ) == $ number_of_digits ) { return $ s ; } else { for ( $ i = 9 ; $ i >= 2 ; $ i -- ) { $ ctr = $ freq [ $ i ] ; while ( $ ctr -- ) { $ t . = chr ( $ i + 48 ) ; } } return $ t ; } } $ s = "1280" ; echo getNumber ( $ s ) ; ? >