Check if a number is sandwiched between primes
Last Updated :
18 May, 2023
A number is said to be sandwiched between primes if the number just after it and the number just before are Prime numbers. So, a sandwiched number is between two prime numbers.
Given a number n, we need to check whether this number is sandwiched between primes or not.
Examples:
Input : 642
Output : Yes
Explanation : 641 and 643 are both prime numbers
Input : 6
Output : Yes
Explanation : 5 and 7 both are prime numbers
Input : 9
Output : No
Explanation : 8 and 10 both are non-prime numbers
The idea is simple, we check if n-1 and n-2 are prime numbers.
C++
#include<iostream>
#include <cmath>
using namespace std;
bool isPrime( int n)
{
if (n == 0 || n == 1) return false ;
int root = sqrt (n);
for ( int i=2;i<=root;i++)
if (n%i == 0)
return false ;
return true ;
}
bool isSandwitched( int n)
{
return (isPrime(n-1) && isPrime(n+1));
}
int main()
{
int n = 642;
cout << n << " : " ;
if (isSandwitched(n))
cout<< "Yes\n" ;
else
cout<< "No\n" ;
n = 9;
cout<< n << " : " ;
if (isSandwitched(n))
cout << "Yes\n" ;
else
cout << "No\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isPrime( int n)
{
if (n == 0 || n == 1 ) return false ;
int root = ( int )Math.sqrt(n);
for ( int i = 2 ; i <= root; i++)
if (n % i == 0 )
return false ;
return true ;
}
static boolean isSandwitched( int n)
{
return (isPrime(n - 1 ) && isPrime(n + 1 ));
}
public static void main (String[] args)
{
int n = 642 ;
System.out.print ( n + " : " );
if (isSandwitched(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
n = 9 ;
System.out.print(n + " : " );
if (isSandwitched(n))
System.out.println( "Yes" );
else
System.out.println ( "No" );
}
}
|
Python3
import math
def isPrime(n):
if (n = = 0 or n = = 1 ):
return False
root = int (math.sqrt(n))
for i in range ( 2 ,root + 1 ):
if (n % i = = 0 ):
return False
return True
def isSandwitched(n):
return (isPrime(n - 1 ) and isPrime(n + 1 ))
n = 642
print (n , end = " : " )
if (isSandwitched(n)):
print ( "Yes" )
else :
print ( "No" )
n = 9
print (n , end = " : " )
if (isSandwitched(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool isPrime( int n)
{
if (n == 0 || n == 1)
return false ;
int root = ( int )Math.Sqrt(n);
for ( int i = 2; i <= root; i++)
if (n % i == 0)
return false ;
return true ;
}
static bool isSandwitched( int n)
{
return (isPrime(n - 1) && isPrime(n + 1));
}
public static void Main ()
{
int n = 642;
Console.Write( n + " : " );
if (isSandwitched(n))
Console.WriteLine( "Yes" );
else
Console.Write( "No" );
n = 9;
Console.Write(n + " : " );
if (isSandwitched(n))
Console.Write( "Yes" );
else
Console.Write ( "No" );
}
}
|
PHP
<?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\n" ;
else
echo "No\n" ;
$n = 9;
echo $n , " : " ;
if (isSandwitched( $n ))
echo "Yes\n" ;
else
echo "No\n" ;
?>
|
Javascript
<script>
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( "Yes" + "<br/>" );
else
document.write( "No" + "<br/>" );
n = 9;
document.write(n + " : " );
if (isSandwitched(n))
document.write( "Yes" + "<br/>" );
else
document.write ( "No" + "<br/>" );
</script>
|
Output:
642 : Yes
9 : No
Time complexity: O(sqrt(n))
Auxiliary space: O(1)
Using lambda function:
Approach:
- Define an anonymous function is_prime that takes a number and returns True if it is prime and False otherwise.
- Get the input number n.
- Check if n-1 and n+1 are prime by calling the is_prime function.
- If both n-1 and n+1 are prime, print “Yes”. Otherwise, print “No”.
Python3
is_prime = lambda num : all (num % i ! = 0 for i in range ( 2 , int (num * * 0.5 ) + 1 ))
n = 9
if is_prime(n - 1 ) and is_prime(n + 1 ):
print ( "Yes" )
else :
print ( "No" )
|
Time complexity: O(sqrt(n))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...