Check if a number is Primorial Prime or not
Given a positive number N, the task is to check if N is a primorial prime number or not. Print ‘YES’ if N is a primorial prime number otherwise print ‘NO.
Primorial Prime: In Mathematics, A Primorial prime is a prime number of the form pn# + 1 or pn# – 1 , where pn# is the primorial of pn i.e the product of first n prime numbers.
Examples:
Input : N = 5
Output : YES
5 is Primorial prime of the form pn - 1
for n=2, Primorial is 2*3 = 6
and 6-1 =5.
Input : N = 31
Output : YES
31 is Primorial prime of the form pn + 1
for n=3, Primorial is 2*3*5 = 30
and 30+1 = 31.
The First few Primorial primes are:
2, 3, 5, 7, 29, 31, 211, 2309, 2311, 30029
Prerequisite:
Approach:
- Generate all prime number in the range using Sieve of Eratosthenes.
- Check if n is prime or not, If n is not prime Then print No
- Else, starting from first prime (i.e 2 ) start multiplying next prime number and keep checking if product + 1 = n or product – 1 = n or not
- If either product+1=n or product-1=n, then n is a Primorial Prime Otherwise not.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 10000
vector< int > arr;
bool prime[MAX];
void SieveOfEratosthenes()
{
memset (prime, true , sizeof (prime));
for ( int p = 2; p * p < MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2; i < MAX; i += p)
prime[i] = false ;
}
}
for ( int p = 2; p < MAX; p++)
if (prime[p])
arr.push_back(p);
}
bool isPrimorialPrime( long n)
{
if (!prime[n])
return false ;
long long product = 1;
int i = 0;
while (product < n) {
product = product * arr[i];
if (product + 1 == n || product - 1 == n)
return true ;
i++;
}
return false ;
}
int main()
{
SieveOfEratosthenes();
long n = 31;
if (isPrimorialPrime(n))
cout << "YES\n" ;
else
cout << "NO\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG {
static final int MAX = 1000000 ;
static Vector<Integer> arr = new Vector<Integer>();
static boolean [] prime = new boolean [MAX];
static void SieveOfEratosthenes()
{
for ( int i = 0 ; i < MAX; i++)
prime[i] = true ;
for ( int p = 2 ; p * p < MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2 ; i < MAX; i += p)
prime[i] = false ;
}
}
for ( int p = 2 ; p < MAX; p++)
if (prime[p])
arr.add(p);
}
static boolean isPrimorialPrime( int n)
{
if (!prime[n])
return false ;
long product = 1 ;
int i = 0 ;
while (product < n) {
product = product * arr.get(i);
if (product + 1 == n || product - 1 == n)
return true ;
i++;
}
return false ;
}
public static void main(String[] args)
{
SieveOfEratosthenes();
int n = 31 ;
if (isPrimorialPrime(n))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python 3
from math import sqrt
MAX = 100000
prime = [ True ] * MAX
arr = []
def SieveOfEratosthenes() :
for p in range ( 2 , int (sqrt( MAX )) + 1 ) :
if prime[p] = = True :
for i in range (p * 2 , MAX , p) :
prime[i] = False
for p in range ( 2 , MAX ) :
if prime[p] :
arr.append(p)
def isPrimorialPrime(n) :
if not prime[n] :
return False
product, i = 1 , 0
while product < n :
product * = arr[i]
if product + 1 = = n or product - 1 = = n :
return True
i + = 1
return False
if __name__ = = "__main__" :
SieveOfEratosthenes()
n = 31
if (isPrimorialPrime(n)) :
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public const int MAX = 1000000;
public static List< int > arr = new List< int >();
public static bool [] prime = new bool [MAX];
public static void SieveOfEratosthenes()
{
for ( int i = 0; i < MAX; i++)
{
prime[i] = true ;
}
for ( int p = 2; p * p < MAX; p++)
{
if (prime[p] == true )
{
for ( int i = p * 2; i < MAX; i += p)
{
prime[i] = false ;
}
}
}
for ( int p = 2; p < MAX; p++)
{
if (prime[p])
{
arr.Add(p);
}
}
}
public static bool isPrimorialPrime( int n)
{
if (!prime[n])
{
return false ;
}
long product = 1;
int i = 0;
while (product < n)
{
product = product * arr[i];
if (product + 1 == n || product - 1 == n)
{
return true ;
}
i++;
}
return false ;
}
public static void Main( string [] args)
{
SieveOfEratosthenes();
int n = 31;
if (isPrimorialPrime(n))
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
PHP
<?php
$MAX = 100000;
$prime = array_fill (0, $MAX , true);
$arr = array ();
function SieveOfEratosthenes()
{
global $MAX , $prime , $arr ;
for ( $p = 2; $p <= (int)(sqrt( $MAX )); $p ++)
{
if ( $prime [ $p ] == true)
for ( $i = $p * 2; $i < $MAX ; $i += $p )
$prime [ $i ] = false;
}
for ( $p = 2; $p < $MAX ; $p ++)
if ( $prime [ $p ])
array_push ( $arr , $p );
}
function isPrimorialPrime( $n )
{
global $MAX , $prime , $arr ;
if (! $prime [ $n ])
return false;
$product = 1;
$i = 0;
while ( $product < $n )
{
$product *= $arr [ $i ];
if ( $product + 1 == $n ||
$product - 1 == $n )
return true;
$i += 1;
}
return false;
}
SieveOfEratosthenes();
$n = 31;
if (isPrimorialPrime( $n ))
print ( "YES" );
else
print ( "NO" );
|
Javascript
<script>
var MAX = 10000;
var arr = [];
var prime = Array(MAX).fill( true );
function SieveOfEratosthenes()
{
for ( var p = 2; p * p < MAX; p++) {
if (prime[p] == true ) {
for ( var i = p * 2; i < MAX; i += p)
prime[i] = false ;
}
}
for ( var p = 2; p < MAX; p++)
if (prime[p])
arr.push(p);
}
function isPrimorialPrime(n)
{
if (!prime[n])
return false ;
var product = 1;
var i = 0;
while (product < n) {
product = product * arr[i];
if (product + 1 == n || product - 1 == n)
return true ;
i++;
}
return false ;
}
SieveOfEratosthenes();
var n = 31;
if (isPrimorialPrime(n))
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(n + MAX3/2)
Auxiliary Space: O(MAX)
Last Updated :
25 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...