Suppose we have a number n, we have to check whether n is a primorial prime or not. A number is said to be a primorial prime when it is a prime number of the form pN# + 1 or pN# – 1 , where pN# indicates the primorial of pN such that the product of first N prime numbers.
So, if the input is like 29, then the output will be True as 29 is Primorial prime of the form pN - 1 if N=3, Primorial is 2*3*5 = 30 and 30-1 = 29.
To solve this, we will follow these steps −
- MAX := 100000
- prime := A list of size MAX and fill with True
- arr := a new list
- Define a function SieveOfEratosthenes() . This will take
- for pri in range 2 to int(square root of (MAX)) + 1, do
- if prime[pri] is same as True, then
- for i in range pri * 2 to MAX, update in each step by pri, do
- prime[i] := False
- for i in range pri * 2 to MAX, update in each step by pri, do
- if prime[pri] is same as True, then
- for pri in range 2 to MAX, do
- if prime[pri] is non-zero, then
- insert pri at the end of arr
- if prime[pri] is non-zero, then
- From the main method, do the following −
- if prime[n] is zero, then
- return False
- product := 1, i := 0
- while product < n, do
- product := product * arr[i]
- if product + 1 is same as n or product - 1 is same as n, then
- return True
- i := i + 1
- return False
Example
Let us see the following implementation to get better understanding −
from math import sqrt MAX = 100000 prime = [True] * MAX arr = [] def SieveOfEratosthenes() : for pri in range(2, int(sqrt(MAX)) + 1) : if prime[pri] == True : for i in range(pri * 2 , MAX, pri) : prime[i] = False for pri in range(2, MAX) : if prime[pri] : arr.append(pri) def check_primorial_prime(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 SieveOfEratosthenes() n = 29 print(check_primorial_prime(n))
Input
29
Output
True