# Balanced Prime Number in Java

In this section, we will discuss what is balance prime number and how to find balanced prime number through a Java program.

## Balance Prime Number

A balanced prime number is a prime number that is equal to the average of the immediate next and immediate previous prime numbers.

Let's understand it through examples.

Example 1:

Input: 13

Output: 13 is not a balanced prime number.

Explanation: Prime number that is immediately next to the number 13 is 17, and the prime number that is immediately previous to the number 13 is 11, and its average is (11 + 17) / 2 = 28 / 2 = 14, which is not equal to 13. Hence, 13 is not a balanced prime number.

Example 2:

Input: 53.

Output: 53 is a balanced prime number.

Explanation: Prime number that is immediately next to the number 53 is 59, and the prime number that is immediately previous to the number 53 is 47, and its average is (47 + 59) / 2 = 106 / 2 = 53, which is equal to the input number. Hence, 53 is a balanced prime number.

## Naive Approach

In this approach, we will use two separate loops. One for computing the immediately previous prime number and the other for computing the immediately next prime number by taking the input prime number as a reference. Then, we find the average of the immediate previous and the immediate next prime numbers and compare it with the input number to check whether the average is equal to the input prime number or not. Observe the following program.

FileName: BalancedPrime.java

Output:

```Total number of balanced primes from 1 to 200 is:
5 53 157 173
```

Time Complexity: If we consider a prime number num, we also assume that the immediate previous prime number is m distance away from num, and the immediate next prime number is n distance away from num. Also, assume that the largest number encountered in search of the immediate previous prime number is k, and the largest number encountered in search of the immediate next prime number is l. Therefore, the time complexity of the program is O(m + n ) for each num.

It is irritating that for each number, we have to check each previous number and the next numbers for computing the immediate next and immediate previous prime numbers. To avoid that, we can use the sieve of Eratosthenes. Observe the following.

## Using Sieve of Eratosthenes Algorithm

FileName: BalancedPrime1.java

Output:

```Total number of balanced primes from 1 to 200 is:
5 53 157 173
```

Time Complexity: For any prime number num, the time complexity of the above program is O(m + n + k.log(k)), where m is the gap between the immediate previous prime number and num and n is the gap between num and the immediate next prime number. k is the range for which the sieve is computed.

### Points to Remember

• For a prime number num, if the average of the immediate next prime number and the immediate smaller prime number is smaller than the number num, then num is called a weak prime number.
• For a prime number num, if the average of the immediate next prime number and the immediate smaller prime number is greater than the number num, then num is called a strong prime number.

## Help Others, Please Share   