Check if N is a Balanced Prime number or not

Given a positive integer N, the task is to check if N is a Balanced Prime number or not.

In number theory, a Balanced Prime  is a prime number with equal-sized prime gaps above and below it, so that it is equal to the arithmetic mean of the nearest primes above and below. Or to put it algebraically, given a prime number p<sub>n</sub>, where n is its index in the ordered set of prime numbers,
 

\Huge p_n=  \frac{p_{n-1}+p_{n+1}}{2}

First  few balanced prime are 5, 53, 157, 173……

Examples:



Input: N = 5
Output: Yes
5 is 3rd prime number, the arithmetic mean of 2nd and 4th prime number i.e. 3 and 7 is 5.
so 5 is a Balanced prime.

Input: N = 11
Output: No

 

Approach:

  • If N is not a prime number or it is the first prime number i.e. 2 then print No.
  • Else find the primes closest to N (one on the left and one on the right) and store their arithmetic mean in mean.
    • If N == mean then print Yes.
    • Else print No.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if a
// given number is Balanced prime
#include <bits/stdc++.h>
using namespace std;
  
// Utility 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 that returns true
// if n is a Balanced prime
bool isBalancedPrime(int n)
{
  
    // If n is not a prime number or
    // n is the first prime then 
    // return false
    if (!isPrime(n) || n == 2)
        return false;
  
    // Initialize previous_prime to
    // n - 1 and next_prime to n + 1
    int previous_prime = n - 1;
    int next_prime = n + 1;
  
    // Find next prime number
    while (!isPrime(next_prime))
        next_prime++;
  
    // Find previous prime number
    while (!isPrime(previous_prime))
        previous_prime--;
  
    // Arithmetic mean
    int mean = (previous_prime +
                next_prime) / 2;
  
    // If n is a weak prime
    if (n == mean)
        return true;
    else
        return false;
}
  
// Driver code
int main()
{
    int n = 53;
  
    if (isBalancedPrime(n))
        cout << "Yes";
    else
        cout << "No";
      
    return 0;
}
  
// This code is contributed by himanshu77

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if a 
// given number is Balanced prime
class GFG{
      
// Utility 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 += 6)
        if (n % i == 0 || 
            n % (i + 2) == 0)
            return false;
    return true;
}
  
// Function that returns true 
// if n is a Balanced prime     
static boolean isBalancedPrime(int n)
{
      
    // If n is not a prime number 
    // or n is the first prime 
    // then return false 
    if (!isPrime(n) || n == 2
        return false
  
    // Initialize previous_prime to 
    // n - 1 and next_prime to n + 1 
    int previous_prime = n - 1
    int next_prime = n + 1
  
    // Find next prime number 
    while (!isPrime(next_prime)) 
        next_prime++; 
  
    // Find previous prime number 
    while (!isPrime(previous_prime)) 
        previous_prime--; 
  
    // Arithmetic mean 
    int mean = (previous_prime + 
                next_prime) / 2
  
    // If n is a weak prime 
    if (n == mean) 
        return true
    else
        return false
}
      
// Driver code
public static void main(String[] args) 
    int n = 53
  
    if (isBalancedPrime(n)) 
        System.out.println("Yes"); 
    else
        System.out.println("No"); 
}
  
// This code is contributed by stutipathak31jan

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to check if a 
# given number is Balanced prime 
  
# 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 += 6
          
    return True
  
# Function that returns true 
# if n is a Balanced prime 
def isBalancedPrime(n):
      
    # If n is not a prime number
    # or n is the first prime 
    # then return false
    if not isPrime(n) or n == 2:
        return False
      
    # Initialize previous_prime to  
    # n - 1 and next_prime to n + 1
    previous_prime = n - 1
    next_prime = n + 1
      
    # Find next prime number
    while not isPrime(next_prime):
        next_prime += 1
          
    # Find previous prime number 
    while not isPrime(previous_prime):
        previous_prime -= 1
          
    # Arithmetic mean 
    mean = (previous_prime + 
            next_prime) / 2
      
    # If n is a weak prime
    if n == mean:
        return True
    else:
        return False
  
# Driver code
n = 53
  
if isBalancedPrime(n):
    print("Yes")
else:
    print("No")
  
# This code is contributed by stutipathak31jan

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if a
// given number is Balanced  prime
using System;
  
class GFG {
  
    // Utility 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 that returns true
    // if n is a Balanced prime
    static bool isBalancedPrime(int n)
    {
  
        // If n is not a prime number or
        // n is the first prime then return false
        if (!isPrime(n) || n == 2)
            return false;
  
        // Initialize previous_prime to n - 1
        // and next_prime to n + 1
        int previous_prime = n - 1;
        int next_prime = n + 1;
  
        // Find next prime number
        while (!isPrime(next_prime))
            next_prime++;
  
        // Find previous prime number
        while (!isPrime(previous_prime))
            previous_prime--;
  
        // Arithmetic mean
        int mean = (previous_prime
                    + next_prime)
                   / 2;
  
        // If n is a weak prime
        if (n == mean)
            return true;
        else
            return false;
    }
  
    // Driver code
    public static void Main()
    {
        int n = 53;
  
        if (isBalancedPrime(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.