Largest number less than N whose each digit is prime number | Number is given as string . ; We stop traversing digits , once it become smaller than current number . For that purpose we use small variable . ; Array indicating if index i ( represents a digit ) is prime or not . ; Store largest ; If there is only one character , return the largest prime less than the number ; If number starts with 1 , return number consisting of 7 ; Traversing each digit from right to left Continue traversing till the number we are forming will become less . ; If digit is prime , copy it simply . ; If not prime , copy the largest prime less than current number ; If not prime , and there is no largest prime less than current prime ; Make current digit as 7 Go left of the digit and make it largest prime less than number . Continue do that until we found a digit which has some largest prime less than it ; If the given number is itself a prime . ; Make last digit as highest prime less than given digit . ; If there is no highest prime less than current digit . ; Once one digit become less than any digit of input replace 7 ( largest 1 digit prime ) till the end of digits of number ; If number include 0 in the beginning , ignore them . Case like 2200 ; Driver Program
def PrimeDigitNumber ( N , size ) : NEW_LINE INDENT ans = [ " " ] * size NEW_LINE ns = 0 ; NEW_LINE small = 0 ; NEW_LINE p = [ 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 , 0 ] NEW_LINE prevprime = [ 0 , 0 , 0 , 2 , 3 , 3 , 5 , 5 , 7 , 7 ] NEW_LINE if ( size == 1 ) : NEW_LINE INDENT ans [ 0 ] = prevprime [ ord ( N [ 0 ] ) - ord ( '0' ) ] + ord ( '0' ) ; NEW_LINE ans [ 1 ] = ' ' ; NEW_LINE return ' ' . join ( ans ) ; NEW_LINE DEDENT if ( N [ 0 ] == '1' ) : NEW_LINE INDENT for i in range ( size - 1 ) : NEW_LINE INDENT ans [ i ] = '7' NEW_LINE DEDENT ans [ size - 1 ] = ' ' ; NEW_LINE return ' ' . join ( ans ) NEW_LINE DEDENT i = 0 NEW_LINE while ( i < size and small == 0 ) : NEW_LINE INDENT if ( p [ ord ( N [ i ] ) - ord ( '0' ) ] == 1 ) : NEW_LINE INDENT ans [ ns ] = N [ i ] NEW_LINE ns += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( p [ ord ( N [ i ] ) - ord ( '0' ) ] == 0 and prevprime [ ord ( N [ i ] ) - ord ( '0' ) ] != 0 ) : NEW_LINE INDENT ans [ ns ] = prevprime [ ord ( N [ i ] ) - ord ( '0' ) ] + ord ( '0' ) ; NEW_LINE small = 1 NEW_LINE ns += 1 NEW_LINE DEDENT elif ( p [ ord ( N [ i ] ) - ord ( '0' ) ] == 0 and prevprime [ ord ( N [ i ] ) - ord ( '0' ) ] == 0 ) : NEW_LINE INDENT j = i ; NEW_LINE while ( j > 0 and p [ ord ( N [ j ] ) - ord ( '0' ) ] == 0 and prevprime [ ord ( N [ j ] ) - ord ( '0' ) ] == 0 ) : NEW_LINE INDENT ans [ j ] = N [ j ] = '7' ; NEW_LINE N [ j - 1 ] = prevprime [ ord ( N [ j - 1 ] ) - ord ( '0' ) ] + ord ( '0' ) ; NEW_LINE ans [ j - 1 ] = N [ j - 1 ] ; NEW_LINE small = 1 ; NEW_LINE j -= 1 NEW_LINE DEDENT i = ns NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT if ( small == 0 ) : NEW_LINE INDENT if ( prevprime [ ord ( N [ size - 1 ] ) - ord ( '0' ) ] + ord ( '0' ) != ord ( '0' ) ) : NEW_LINE INDENT ans [ size - 1 ] = prevprime [ ord ( N [ size - 1 ] ) - ord ( '0' ) ] + ord ( '0' ) ; NEW_LINE DEDENT else : NEW_LINE INDENT j = size - 1 ; NEW_LINE while ( j > 0 and prevprime [ ord ( N [ j ] ) - ord ( '0' ) ] == 0 ) : NEW_LINE INDENT ans [ j ] = N [ j ] = '7' ; NEW_LINE N [ j - 1 ] = prevprime [ ord ( N [ j - 1 ] ) - ord ( '0' ) ] + ord ( '0' ) ; NEW_LINE ans [ j - 1 ] = N [ j - 1 ] ; NEW_LINE small = 1 ; NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT DEDENT while ( ns < size ) : NEW_LINE INDENT ans [ ns ] = '7' NEW_LINE ns += 1 NEW_LINE DEDENT ans [ ns ] = ' ' ; NEW_LINE k = 0 ; NEW_LINE while ( ans [ k ] == '0' ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT return ( ans + k ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = "1000" ; NEW_LINE size = len ( N ) ; NEW_LINE print ( PrimeDigitNumber ( N , size ) ) NEW_LINE DEDENT