# Check whether the given numbers are Cousin prime or not 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 a pair of integers. We have to check whether they are cousin primes or not. Two numbers are said to be cousin primes when both are primes and differ by 4.

So, if the input is like pair = (19,23), then the output will be True as these are two primes and their difference is 4 so they are cousin primes.

To solve this, we will follow these steps −

• if difference between two elements is not 4, then
• return False
• return true when both are prime, otherwise false

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 solve(pair) :
if not abs(pair-pair)== 4:
return False
return isPrime(pair) and isPrime(pair)

pair = (19,23)
print(solve(pair))

## Input

(19,23)


## Output

True