Check if a number is sandwiched between primes | 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 code
function isPrime ( n ) { if ( n == 0 n == 1 ) return false ; let root = Math . sqrt ( n ) ; for ( let i = 2 ; i <= root ; i ++ ) if ( n % i == 0 ) return false ; return true ; } function isSandwitched ( n ) { return ( isPrime ( n - 1 ) && isPrime ( n + 1 ) ) ; } let n = 642 ; document . write ( n + " " ) ; if ( isSandwitched ( n ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ; n = 9 ; document . write ( n + " " ) ; if ( isSandwitched ( n ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;