# Check if a number is Primorial Prime or not in C++

## Concept

With respect of given positive number n, the task is to verify if n is a primorial prime number or not. We have to print ‘YES’ if n is a primorial prime number otherwise print ‘NO.

Primorial Prime − With respect of Mathematics, a Primorial prime is defined as a prime number of the form pN# + 1 or pN# – 1 , where pN# is the primorial of pN such that the product of first N prime numbers.

Input − n = 7

Output − YES

7 is Primorial prime of the form pN + 1 for N=2, Primorial is 2*3 = 6 and 6+1 =7.

Input − n = 29

Output − YES

29 is Primorial prime of the form pN - 1 for N=3, Primorial is 2*3*5 = 30 and 30-1 = 29.

In the following, the First few Primorial primes are displayed − 2, 3, 5, 7, 29, 31, 211, 2309, 2311, 30029

## Approach

• We have to generate all prime number in the range by applying Sieve of Eratosthenes.

• Verify if N is prime or not, If N is not prime, then print No

• Otherwise, beginning from first prime (i.e 2 ) start multiplying next prime number and keep verifying if product + 1 = N or product – 1 = N or not

• It has been seen that if either product+1=N or product-1=N, then N is a Primorial Prime otherwise not.

## Example

Live Demo

// CPP program to check Primorial Prime
#include <bits/stdc++.h>
using namespace std;
#define MAX 10000
vector<int> arr1;
bool prime1[MAX];
void SieveOfEratosthenes1(){
memset(prime1, true, sizeof(prime1));
for (int p = 2; p * p < MAX; p++) {
if (prime1[p] == true) {
for (int i = p * 2; i < MAX; i += p)
prime1[i] = false;
}
}
for (int p = 2; p < MAX; p++)
if (prime1[p])
arr1.push_back(p);
}
bool isPrimorialPrime1(long n){
// If n is not prime Number
// return flase
if (!prime1[n])
return false;
long long product1 = 1;
int i = 0;
while (product1 < n) {
product1 = product1 * arr1[i];
if (product1 + 1 == n || product1 - 1 == n)
return true;
i++;
}
return false;
}
// Driver code
int main(){
SieveOfEratosthenes1();
long n = 29;
// Check if n is Primorial Prime
if (isPrimorialPrime1(n))
cout << "YES\n";
else
cout << "NO\n";
return 0;
}

## Output

YES