Largest number with prime digits | check if character is prime ; replace with previous prime character ; if 2 erase s [ i ] and replace next with 7 ; find first non prime char ; find first char greater than 2 ; like 20 ; like 7721 ; replace remaining with 7 ; Driver code
< ? php function isPrime ( $ c ) { return ( $ c == '2' $ c == '3' $ c == '5' $ c == '7' ) ? 1 : 0 ; } function decrease ( $ s , $ i ) { if ( $ s [ $ i ] <= '2' ) { $ s [ $ i ] = ' * ' ; $ a = str_split ( $ s ) ; $ s = " " ; for ( $ h = 0 ; $ h < count ( $ a ) ; $ h ++ ) if ( $ a [ $ h ] != ' * ' ) $ s = $ s . $ a [ $ h ] ; $ s [ $ i ] = '7' ; } else if ( $ s [ $ i ] == '3' ) $ s [ $ i ] = '2' ; else if ( $ s [ $ i ] <= '5' ) $ s [ $ i ] = '3' ; else if ( $ s [ $ i ] <= '7' ) $ s [ $ i ] = '5' ; else $ s [ $ i ] = '7' ; return $ s ; } function primeDigits ( $ s ) { for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( isPrime ( $ s [ $ i ] ) == 0 ) { while ( $ i >= 0 && $ s [ $ i ] <= '2' ) -- $ i ; if ( $ i < 0 ) { $ i = 0 ; $ s = decrease ( $ s , $ i ) ; } else $ s = decrease ( $ s , $ i ) ; for ( $ j = $ i + 1 ; $ j < strlen ( $ s ) ; $ j ++ ) $ s [ $ j ] = '7' ; break ; } } return $ s ; } $ s = "45" ; echo primeDigits ( $ s ) . " STRNEWLINE " ; $ s = "1000" ; echo primeDigits ( $ s ) . " STRNEWLINE " ; $ s = "7721" ; echo primeDigits ( $ s ) . " STRNEWLINE " ; $ s = "7221" ; echo primeDigits ( $ s ) . " STRNEWLINE " ; $ s = "74545678912345689748593275897894708927680" ; echo primeDigits ( $ s ) ; ? >