Check if number can be made prime by deleting a single digit | Function to check if N is prime from builtins import range ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to delete character at index i from given String str ; Deletes character at position 4 ; Function to check if a number becomes prime by deleting any digit ; Converting the number to String ; length of String ; number should not be of single digit ; Loop to find all numbers after deleting a single digit ; Deleting ith character from the String ; converting String to int ; Driver Code
def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( 5 , int ( n ** 1 / 2 ) , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def deleteIth ( str , i ) : NEW_LINE INDENT str = str [ 0 : i ] + str [ i + 1 : ] ; NEW_LINE return str ; NEW_LINE DEDENT def isPrimePossible ( N ) : NEW_LINE INDENT s = str ( N ) ; NEW_LINE l = len ( s ) ; NEW_LINE if ( l < 2 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( l ) : NEW_LINE INDENT str1 = deleteIth ( s , i ) ; NEW_LINE num = int ( str1 ) ; NEW_LINE if ( isPrime ( num ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 610 ; NEW_LINE if ( isPrimePossible ( N ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT