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
< ? php function isPrime ( $ n ) { if ( $ n == 0 $ n == 1 ) return false ; $ root = sqrt ( $ n ) ; for ( $ i = 2 ; $ i <= $ root ; $ i ++ ) if ( $ n % $ i == 0 ) return false ; return true ; } function isSandwitched ( $ n ) { return ( isPrime ( $ n - 1 ) && isPrime ( $ n + 1 ) ) ; } $ n = 642 ; echo $ n , " ▁ : ▁ " ; if ( isSandwitched ( $ n ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; $ n = 9 ; echo $ n , " ▁ : ▁ " ; if ( isSandwitched ( $ n ) ) echo " Yes STRNEWLINE " ; else echo " No STRNEWLINE " ; ? >