# Check whether a number is Good prime or not

Given a positive integer N, the task is to check whether the given number is good prime or not. If the given number is good prime print ‘YES’ Otherwise Print ‘NO’.

Good Prime: In Mathematics, a good prime is a prime number whose square is greater than the product of any two primes at the same number of positions before and after it in the sequence of primes. In other word, A prime Pn is said to be good prime if it for every 1 <= i < n.

The first few good primes are: 5, 11, 17, 29, 37, 41, 53, 59, 67, 71, 97, 101, 127, 149, 179, 191, 223, ….

Examples:

Input: N = 5
Output: YES
Explanation: 5 is a good prime number
since 5^2 = 25 is greater than 3.7 = 21
and 2.11 = 22.

Input:  N = 20
Output: NO

Approach:

1. Get the number N.

2. Initialise prev_prime = N-1 and next_prime = N+1

3. Iterate the loop while prev_prime is greater than or equal to 2. And check for both next_prime and prev_prime are prime of not using prime number.

4. If both are not prime, then repeat step 2 and 3.

5. If both next_prime and prev_prime are prime, then check N^2 > next_prime . prev_prime or not.

• If Not then number is not good prime and stop the execution and return NO.
• If Yes then repeat the step 2, 3, 4 and 5.

Below is the implementaion of the above approach:

## C++

 `// C++ program to check if a number ` `// is good prime 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 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 to check if the  ` `// given number is Good prime ` `bool` `isGoodprime (``int` `n) ` `{ ` ` `  `    ``// Smallest good prime is 5 ` `    ``// So the number less than 5 ` `    ``// can not be a Good prime ` ` `  `    ``if` `(n < 5) ` `        ``return` `false``; ` ` `  `    ``int` `prev_prime = n - 1; ` `    ``int` `next_prime = n + 1; ` ` `  `    ``while` `(prev_prime >= 2) ` `    ``{ ` `         `  `        ``// Calculate first prime number < n ` `        ``while` `(!isPrime(prev_prime)) ` `        ``{ ` `            ``prev_prime--; ` `        ``} ` ` `  `        ``// Calculate first prime number > n ` `        ``while` `(!isPrime(next_prime)) ` `        ``{ ` `            ``next_prime++; ` `        ``} ` ` `  `        ``// Check if product of next_prime ` `        ``// and prev_prime is less than n^2 ` `        ``if` `((prev_prime * next_prime) >= n * n) ` `            ``return` `false``; ` ` `  `        ``prev_prime -= 1; ` `        ``next_prime += 1; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 11; ` ` `  `    ``if` `(isGoodprime(n)) ` `        ``cout << ``"YES"``; ` `    ``else` `        ``cout << ``"NO"``; ` ` `  `    ``return` `0; ` `} ` ` `  `// This code is contributed by himanshu77 `

## Java

 `// Java program to check if a number is ` `// good prime 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 good prime or not ` `static` `boolean` `isGoodrprime(``int` `n) ` `{ ` ` `  `    ``// Smallest good prime is 5 ` `    ``// So the number less than 5 ` `    ``// can not be a good prime ` ` `  `    ``if` `(n < ``5``) ` `        ``return` `false``; ` ` `  `    ``int` `prev_prime = n - ``1``; ` `    ``int` `next_prime = n + ``1``; ` ` `  `    ``while` `(prev_prime >= ``2``) ` `    ``{ ` `         `  `        ``// Calculate first prime number < n ` `        ``while` `(!isPrime(prev_prime))  ` `        ``{ ` `            ``prev_prime--; ` `        ``} ` ` `  `        ``// Calculate first prime number > n ` `        ``while` `(!isPrime(next_prime)) ` `        ``{ ` `            ``next_prime++; ` `        ``} ` ` `  `        ``// Check if product of next_prime ` `        ``// and prev_prime ` `        ``// is less than n^2 ` `        ``if` `((prev_prime * next_prime) >= n * n) ` `            ``return` `false``; ` ` `  `        ``prev_prime -= ``1``; ` `        ``next_prime += ``1``; ` `    ``} ` `    ``return` `true``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` `    ``int` `n = ``11``; ` `     `  `    ``if` `(isGoodrprime(n)) ` `        ``System.out.println(``"YES"``); ` `    ``else` `        ``System.out.println(``"NO"``); ` `} ` `} ` ` `  `// This code is contributed by amal kumar choubey `

## Python3

 `# Python3 program to check if a number is  ` `# good prime 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 good prime or not  ` `def` `isGoodrPrime(n): ` ` `  `    ``# Declaring variables as global ` `    ``global` `next_prime, prev_prime ` ` `  `    ``# Smallest good prime is 5 ` `    ``# So the number less than 5 ` `    ``# can not be a good prime ` `    ``if``(n < ``5``): ` `        ``return` `False` ` `  `    ``# Initialize previous_prime to n - 1 ` `    ``# and next_prime to n + 1 ` `    ``prev_prime ``=` `n ``-` `1` `    ``next_prime ``=` `n ``+` `1` ` `  `    ``while``(prev_prime >``=` `2``): ` ` `  `        ``# Calculate first prime number < n ` `        ``while` `(``not` `isPrime(prev_prime)): ` `            ``prev_prime ``-``=` `1` ` `  `        ``# Calculate first prime number > n ` `        ``while``(``not` `isPrime(next_prime)): ` `            ``next_prime ``+``=` `1` ` `  `        ``# Check if product of next_prime ` `        ``# and prev_prime ` `        ``# is less than n^2 ` `        ``if``((prev_prime ``*` `next_prime) >``=` `n ``*` `n): ` `            ``return` `False` ` `  `        ``prev_prime ``-``=` `1` `        ``next_prime ``+``=` `1` ` `  `    ``return` `True` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` ` `  `    ``n ``=` `11` ` `  `    ``if``(isGoodrPrime(n)): ` `        ``print``(``"Yes"``) ` `    ``else``: ` `        ``print``(``"No"``) ` ` `  `# This code is contributed by Shivam Singh `

## C#

 `// C# program to check if a number is ` `// good prime  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 good prime or not ` `    ``static` `bool` `isGoodrprime(``int` `n) ` `    ``{ ` ` `  `        ``// Smallest good prime is 5 ` `        ``// So the number less than 5 ` `        ``// can not be a good prime ` ` `  `        ``if` `(n < 5) ` `            ``return` `false``; ` ` `  `        ``int` `prev_prime = n - 1; ` `        ``int` `next_prime = n + 1; ` ` `  `        ``while` `(prev_prime >= 2) { ` `            ``// Calculate first prime number < n ` `            ``while` `(!isPrime(prev_prime)) { ` `                ``prev_prime--; ` `            ``} ` ` `  `            ``// Calculate first prime number > n ` `            ``while` `(!isPrime(next_prime)) { ` `                ``next_prime++; ` `            ``} ` ` `  `            ``// check if product of next_prime ` `            ``// and prev_prime ` `            ``// is less than n^2 ` ` `  `            ``if` `((prev_prime * next_prime) ` `                ``>= n * n) ` `                ``return` `false``; ` ` `  `            ``prev_prime -= 1; ` `            ``next_prime += 1; ` `        ``} ` `        ``return` `true``; ` `    ``} ` ` `  `    ``public` `static` `void` `Main() ` `    ``{ ` ` `  `        ``int` `n = 11; ` `        ``if` `(isGoodrprime(n)) ` `            ``Console.WriteLine(``"YES"``); ` `        ``else` `            ``Console.WriteLine(``"NO"``); ` `    ``} ` `}`

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.