Check if a number is sandwiched between primes | Python Program to check whether a number is sandwiched between two primes or not ; returns true if number n is prime ; 0 and 1 both are non - primes ; finding square root of n ; checking if n has any factor upto square root of n if yes its not prime ; driver Function
import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE return False NEW_LINE root = int ( math . sqrt ( n ) ) NEW_LINE for i in range ( 2 , root + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isSandwitched ( n ) : NEW_LINE INDENT return ( isPrime ( n - 1 ) and isPrime ( n + 1 ) ) NEW_LINE DEDENT n = 642 NEW_LINE print ( n , end = " ▁ : ▁ " ) NEW_LINE if ( isSandwitched ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT n = 9 NEW_LINE print ( n , end = " ▁ : ▁ " ) NEW_LINE if ( isSandwitched ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT