Given a positive number n, the task is to check whether the given number is Interprime or not. If the given number is Interprime print ‘YES’ Otherwise Print ‘NO’.
Interprime : In Mathematics, An interprime is a positive integer representing average of two consecutive odd prime numbers.
The first few Interprimes are –
4, 6, 9, 12, 15, 18, 21, 26, 30, 34, 39, 42, 45, 50, 56, 60, 64, 69, 72, 76, 81, 86, 93, 99
Input : 12
Output : YES
Explanation: 12 is average of consecutive prime number 11 and 13.
Input : 20
Output : NO
A Simple solution of this problem is to generate prime numbers and check if we can get the the given average from consecutive odd prime number or not.
- Start generating prime number ‘p’ form i=3 to the prime number p > n
- if we find average of p and p+1 as the given number n, then stop and Print ‘YES’
- If we don not find such p and p+1 with the given average print ‘NO’.
An Efficient solution is to check average of prime number p1 and p2 only, such that p1 and p2 are consecutive and p1 < n < p2.
- calculate consecutive prime number p1 and p2. such that p1 < n < p2.
- calculate average of p1 and p2.
- Print ‘YES’ if we get the given average Otherwise print ‘NO’.
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 digit cube limit of an integer arrives at fixed point or a limit cycle
- Check if any permutation of array contains sum of every adjacent pair not divisible by 3
- Length of longest increasing prime subsequence from a given array
- Minimize sum of prime numbers added to make an array non-decreasing
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Check if Euler Totient Function is same for a given number and twice of that number
- Length of longest common prime subsequence from two given arrays
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Length of longest subarray having only K distinct Prime Numbers
- Smallest composite number not divisible by first N prime numbers
- Count of subarrays consisting of only prime numbers
- Minimize Array length by repeatedly replacing co-prime pairs with 1
- Balanced Ternary Number System
- Least common element in given two Arithmetic sequences
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.
Improved By : Shashank12