# Check whether given three numbers are adjacent primes in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have three numbers and we have to check whether they are adjacent primes are not. The adjacent primes are prime numbers where no other prime is present between them.

So, if the input is like nums = [5,7,11], then the output will be True.

To solve this, we will follow these steps −

• if any one of these three numbers is not prime them
• return False
• if next prime of x is not same as y, then
• return False
• if next prime of y is not same as z, then
• return False
• return True

Let us see the following implementation to get better understanding −

## Example Code

Live Demo

def isPrime(num):
if num > 1:
for i in range(2, num):
if num % i == 0:
return False
return True
return False

def get_next_prime(num) :
next_prime = num + 1
while not isPrime(next_prime):
next_prime += 1
return next_prime

def solve(x, y, z) :
if isPrime(x) == False or isPrime(y) == False or isPrime(z) == False :
return False

next_prime = get_next_prime(x)
if next_prime != y:
return False

if get_next_prime(y) != z:
return False

return True

nums = [5,7,11]
print(solve(*nums))

## Input

[5,7,11]


## Output

True
Updated on 16-Jan-2021 04:34:09