Check if a number is Primorial Prime or not in Python

A primorial prime is a prime number that can be expressed in the form pN# + 1 or pN# − 1, where pN# represents the primorial (product of the first N prime numbers). For example, if N=3, the primorial is 2×3×5 = 30, so 29 (30−1) and 31 (30+1) are potential primorial primes if they are also prime.

Let's implement a solution to check if a given number is a primorial prime ?

Algorithm Steps

To solve this problem, we will −

  • Use the Sieve of Eratosthenes to find all prime numbers up to a maximum limit
  • Store prime numbers in a list for easy access
  • Check if the input number is prime (prerequisite for being a primorial prime)
  • Calculate primorials by multiplying consecutive primes and check if adding or subtracting 1 gives our target number

Implementation

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

# Initialize the sieve
SieveOfEratosthenes()

# Test with example
n = 29
result = check_primorial_prime(n)
print(f"{n} is primorial prime: {result}")
29 is primorial prime: True

How It Works

Let's trace through the example with n = 29 −

  • First, verify 29 is prime ?
  • Calculate primorials: 2# = 2, 2×3# = 6, 2×3×5# = 30
  • Check: 30 − 1 = 29 ?
  • Since 29 is prime and equals a primorial minus 1, it's a primorial prime

Testing Multiple Numbers

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

# Initialize the sieve
SieveOfEratosthenes()

# Test multiple numbers
test_numbers = [3, 5, 7, 29, 31, 211, 2309, 2311]

for num in test_numbers:
    result = check_primorial_prime(num)
    print(f"{num}: {result}")
3: True
5: True
7: True
29: True
31: True
211: True
2309: True
2311: True

Key Points

  • Primorial: Product of first N prime numbers (2# = 2, 6# = 2×3 = 6, 30# = 2×3×5 = 30)
  • Primorial Prime: A prime number of the form pN# ± 1
  • The algorithm first checks if the number is prime, then verifies if it matches any primorial ± 1
  • Time complexity is efficient due to the Sieve of Eratosthenes preprocessing

Conclusion

This solution efficiently identifies primorial primes by combining the Sieve of Eratosthenes for prime generation with a systematic check of primorial values. The algorithm works by verifying both primality and the primorial relationship simultaneously.

Updated on: 2026-03-25T15:03:27+05:30

897 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements