Related Articles

# Interprime

• Last Updated : 07 May, 2021

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
Examples:

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.
Approach :

1. Start generating prime number ‘p’ form i=3 to the prime number p > n
2. if we find average of p and p+1 as the given number n, then stop and Print ‘YES’

3. 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.
Approach :

1. calculate consecutive prime number p1 and p2. such that p1 < n < p2.

2. calculate average of p1 and p2.

3. Print ‘YES’ if we get the given average Otherwise print ‘NO’.

## C++

 `// CPP program to check if a number is``// interprime  or not` `#include ``using` `namespace` `std;` `// Function to check if a number is prime or not``bool` `isPrime(``int` `n)``{``    ``// Corner cases``    ``if` `(n <= 1)``        ``return` `false``;``    ``if` `(n <= 3)``        ``return` `true``;` `    ``// This is checked so that we can skip``    ``// middle five numbers in below loop``    ``if` `(n % 2 == 0 || n % 3 == 0)``        ``return` `false``;` `    ``for` `(``int` `i = 5; i * i <= n; i = i + 6) {``        ``if` `(n % i == 0 || n % (i + 2) == 0) {``            ``return` `false``;``        ``}``    ``}` `    ``return` `true``;``}` `// Function to check``// if the given number is interprime or not``bool` `isInterprime(``int` `n)``{` `    ``// Smallest Interprime is 4``    ``// So the number less than 4``    ``// can not be a Interprime` `    ``if` `(n < 4)``        ``return` `false``;` `    ``int` `prev_prime = n;``    ``int` `next_prime = n;` `    ``// Calculate first prime number < n``    ``while` `(!isPrime(prev_prime)) {``        ``prev_prime--;``    ``}` `    ``// Calculate first prime number > n``    ``while` `(!isPrime(next_prime)) {``        ``next_prime++;``    ``}` `    ``// Check if prev_prime and next_prime``    ``// have the same average``    ``if` `((prev_prime + next_prime) == 2 * n)``        ``return` `true``;``    ``else``        ``return` `false``;``}` `int` `main()``{``    ``int` `n = 9;``    ``if` `(isInterprime(n))``        ``cout << ``"YES"``;``    ``else``        ``cout << ``"NO"``;` `    ``return` `0;``}`

## Java

 `// JAVA program to check if a number is``// interprime  or not` `class` `GFG {``    ``// Function to check if a number is prime or not``    ``static` `boolean` `isPrime(``int` `n)``    ``{``        ``// Corner cases``        ``if` `(n <= ``1``)``            ``return` `false``;``        ``if` `(n <= ``3``)``            ``return` `true``;` `        ``// This is checked so that we can skip``        ``// middle five numbers in below loop``        ``if` `(n % ``2` `== ``0` `|| n % ``3` `== ``0``)``            ``return` `false``;` `        ``for` `(``int` `i = ``5``; i * i <= n; i = i + ``6``) {``            ``if` `(n % i == ``0` `|| n % (i + ``2``) == ``0``) {``                ``return` `false``;``            ``}``        ``}``        ``return` `true``;``    ``}` `    ``// Function to check``    ``// if the given number is interprime or not``    ``static` `boolean` `isInterprime(``int` `n)``    ``{` `        ``// Smallest Interprime is 4``        ``// So the number less than 4``        ``// can not be a Interprime` `        ``if` `(n < ``4``)``            ``return` `false``;` `        ``int` `prev_prime = n;``        ``int` `next_prime = n;` `        ``// Calculate first prime number < n``        ``while` `(!isPrime(prev_prime)) {``            ``prev_prime--;``        ``}` `        ``// Calculate first prime number > n``        ``while` `(!isPrime(next_prime)) {``            ``next_prime++;``        ``}` `        ``// check if next_prime and prev_prime``        ``// have the same average``        ``if` `((prev_prime + next_prime) == ``2` `* n)``            ``return` `true``;``        ``else``            ``return` `false``;``    ``}` `    ``public` `static` `void` `main(String[] args)``    ``{` `        ``int` `n = ``9``;``        ``if` `(isInterprime(n))``            ``System.out.println(``"YES"``);``        ``else``            ``System.out.println(``"NO"``);``    ``}``}`

## Python3

 `# Python 3 program to check if a number is  ``# interprime  or not ``    ` `# Utility function to check ``# if a number is prime or not ``def` `isPrime(n) :  ` `    ``# Corner cases  ``    ``if` `(n <``=` `1``) :  ``        ``return` `False``    ``if` `(n <``=` `3``) :  ``        ``return` `True``    ` `    ``# This is checked so that we can skip  ``    ``# middle five numbers in below loop  ``    ``if` `(n ``%` `2` `=``=` `0` `or` `n ``%` `3` `=``=` `0``) :  ``        ``return` `False``    ` `    ``i ``=` `5``    ``while` `(i ``*` `i <``=` `n) :  ``        ``if` `(n ``%` `i ``=``=` `0` `or` `n ``%` `(i ``+` `2``) ``=``=` `0``) :  ``            ``return` `False``        ``i ``=` `i ``+` `6``    ` `    ``return` `True` `# Function to check``# if the given number is interprime or not``def` `isInterprime( n):` `    ` `     ``# Smallest Interprime is 4``     ``# So the number less than 4``     ``# can not be a Interprime``     ` `     ``if` `(n < ``4``):``        ``return` `False``     ` `     ` `     ``prev_prime ``=` `n``     ``next_prime ``=` `n``     ` `     ``# Calculate first prime number < n``     ``while` `(isPrime(prev_prime)``=``=` `0``):``         ``prev_prime ``=` `prev_prime``-``1``    ` `     ` `     ``# Calculate first prime number > n``     ``while` `(isPrime(next_prime)``=``=` `0``):``         ``next_prime ``=` `next_prime ``+` `1``     ` `     ``# check if next_prime and prev_prime``     ``# have the same average``     ``if` `((prev_prime ``+` `next_prime)``=``=` `2` `*` `n):``         ``return` `True``     ``else``:``        ``return` `False`   `        ` `n ``=` `9``if``(isInterprime(n)):``    ``print``(``"YES"``)``else``:``    ``print``(``"NO"``)`

## C#

 `// C# program to check if a number is``// interprime  or not` `using` `System;``class` `GFG {``    ``// Function to check if a number is prime or not``    ``static` `bool` `isPrime(``int` `n)``    ``{``        ``// Corner cases``        ``if` `(n <= 1)``            ``return` `false``;``        ``if` `(n <= 3)``            ``return` `true``;` `        ``// This is checked so that we can skip``        ``// middle five numbers in below loop``        ``if` `(n % 2 == 0 || n % 3 == 0)``            ``return` `false``;` `        ``for` `(``int` `i = 5; i * i <= n; i = i + 6) {``            ``if` `(n % i == 0 || n % (i + 2) == 0) {``                ``return` `false``;``            ``}``        ``}``        ``return` `true``;``    ``}` `    ``// Function to check``    ``// if the given number is interprime or not``    ``static` `bool` `isInterprime(``int` `n)``    ``{` `        ``// Smallest Interprime is 4``        ``// So the number less than 4``        ``// can not be a Interprime` `        ``if` `(n < 4)``            ``return` `false``;` `        ``int` `prev_prime = n;``        ``int` `next_prime = n;` `        ``// Calculate first prime number < n``        ``while` `(!isPrime(prev_prime)) {``            ``prev_prime--;``        ``}` `        ``// Calculate first prime number > n``        ``while` `(!isPrime(next_prime)) {``            ``next_prime++;``        ``}` `        ``// check if next_prime and prev_prime``        ``// have the same average``        ``if` `((prev_prime + next_prime) == 2 * n)``            ``return` `true``;``        ``else``            ``return` `false``;``    ``}` `    ``public` `static` `void` `Main()``    ``{` `        ``int` `n = 9;``        ``if` `(isInterprime(n))``            ``Console.WriteLine(``"YES"``);``        ``else``            ``Console.WriteLine(``"NO"``);``    ``}``}`

## PHP

 ` n``    ``while` `(!isPrime(``\$next_prime``))``    ``{``        ``\$next_prime``++;``    ``}` `    ``// Check if prev_prime and``    ``// next_prime have the same average``    ``if` `((``\$prev_prime` `+``         ``\$next_prime``) == 2 * ``\$n``)``        ``return` `true;``    ``else``        ``return` `false;``}` `// Driver Code``\$n` `= 9;``if` `(isInterprime(``\$n``))``    ``echo` `"YES"``;``else``    ``echo` `"NO"``;``    ` `// This code is contributed``// by Shashank``?>`

## Javascript

 ``

Output:

`YES`

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up