Given two positive integer n1 and n2, the task is to check if both are Cousin primes or not. Print ‘YES’ if the both the numbers are Cousin primes otherwise print ‘NO’.
Cousin primes: In Mathematics, Cousin primes are prime numbers that differ by 4. Suppose ‘p’ is a prime number and if ( p + 4) is also a prime number then both the prime numbers will be called as cousin primes.
The Cousin primes below 100 are:
(3, 7), (7, 11), (13, 17), (19, 23), (37, 41), (43, 47), (67, 71), (79, 83), (97, 101)
Input: n1 = 7, n2 = 11 Output: YES Both are prime numbers and they differ by 4. Input: n1 = 5, n2 = 11 Output: NO Both are prime numbers but they differ by 6.
Approach: A Simple Solution is to check if both number are primes and differ by 4 or not. If they are primes and differ by 4 then they will be cousin primes, otherwise not.
Below is the implementation of above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Check if a prime number can be expressed as sum of two Prime Numbers
- Check if two numbers are co-prime or not
- Check if a number can be written as a sum of 'k' prime numbers
- Check if each element of the given array is the product of exactly K prime numbers
- Check if product of array containing prime numbers is a perfect square
- Check if elements of array can be made equal by multiplying given prime numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print the nearest prime number formed by adding prime numbers to N
- Quick ways to check for Prime and find next Prime in Java
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Permutation of first N positive integers such that prime numbers are at prime indices
- Count all prime numbers in a given range whose sum of digits is also prime
- Print prime numbers with prime sum of digits in an array
- Check whether the sum of prime elements of the array is prime or not
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Sum of prime numbers without odd prime digits
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.