
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check whether the given numbers are Cousin prime or not in Python
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
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[0]-pair[1])== 4: return False return isPrime(pair[0]) and isPrime(pair[1]) pair = (19,23) print(solve(pair))
Input
(19,23)
Output
True
- Related Articles
- Check whether the given number is Wagstaff prime or not in Python
- Check whether the sum of prime elements of the array is prime or not in Python
- Check whether the frequencies of all the characters in a string are prime or not in Python
- Check whether N is a Dihedral Prime Number or not in Python
- Check whether triangle is valid or not if sides are given in Python
- How to check whether a number is prime or not using Python?
- Check whether the given number is Euclid Number or not in Python
- Check whether two strings are equivalent or not according to given condition in Python
- Check whether given three numbers are adjacent primes in Python
- Check whether the sum of absolute difference of adjacent digits is Prime or not in Python
- Program to check whether given graph is bipartite or not in Python
- Program to check whether given password meets criteria or not in Python
- Program to check whether every rotation of a number is prime or not in Python
- Write a Golang program to check whether a given number is prime number or not
- Program to check whether given words are maintaining given pattern or not in C++

Advertisements