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
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 i += 1 NEW_LINE