Skip to main content

Check if a number is primorial prime or not

In this article, we'll explore the concept of Primorial Prime and how to check if a given number is a Primorial Prime or not. Primorial Prime is a specific type of prime number that is related to the primorial function. The primorial of a number 'n' (denoted as n#) is the product of all prime numbers less than or equal to 'n'. A Primorial Prime is a number that is both prime and also equal to the primorial of a certain number.

Problem Statement

Given a number 'num', we need to determine if it is a Primorial Prime or not. To do this, we'll first find all the prime numbers up to a certain limit using the Sieve of Eratosthenes algorithm. Then, for each number 'num', we'll check if it satisfies the conditions of being a Primorial Prime.

Example

Let's take 'num = 29' as an example. We'll find the primorial of 29 (29#) and check if it is a prime number or not.

The prime numbers up to 29 are {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}. Therefore, 29# = 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 = 6469693230.

Now, we check if 6469693230 is a prime number. In this case, it is not, so 29 is not a Primorial Prime.

Pseudocode

// Function to find all prime numbers up to SIZE using Sieve of Eratosthenes
function sieve_of_eratosthenes(collection):
    for i from 0 to SIZE:
        collection[i] = 1

    collection[0] = 0
    collection[1] = 0

    for i from 2 to sqrt(SIZE):
        if collection[i] == 1:
            for j from i * i to SIZE with step i:
                collection[j] = 0

// Function to check if a number 'num' is Primorial Prime
function is_primorial_prime(collection, num):
    status = 0

    if num > 1 and collection[num] == 1:
        product = 1
        i = 2

        while status == 0 and i <= num and product <= num + 1:
            if collection[i] == 1:
                if product + 1 == num or product - 1 == num:
                    status = 1
                else:
                    product *= i
            i += 1

    if status == 1:
        print("[", num, "] Is Primorial Prime")
    else:
        print("[", num, "] Is Not Primorial Prime")

// Main function
function main():
    // Create an array to store prime number status
    collection = array of size SIZE

    // Find all prime numbers using Sieve of Eratosthenes
    sieve_of_eratosthenes(collection)

    // Test cases
    is_primorial_prime(collection, 7)
    is_primorial_prime(collection, 107)
    is_primorial_prime(collection, 13)
    is_primorial_prime(collection, 1319)
    is_primorial_prime(collection, 211)
    is_primorial_prime(collection, 373)
    is_primorial_prime(collection, 29)

// Call the main function to start the program
main()
  1. Define a constant SIZE to represent the range of numbers to consider.
  2. Implement the sieve_of_eratosthenes function to find all prime numbers up to SIZE.
  3. Implement the is_primorial_prime function to check if a number 'num' is a Primorial Prime using the previously generated prime numbers.
  4. In the main function, call sieve_of_eratosthenes to initialize the prime number status.
  5. Test is_primorial_prime function with various 'num' values.

Algorithm

  1. Create an array 'collection' of size SIZE to store the status of prime numbers.
  2. Initialize all elements of 'collection' to 1 (considering all numbers as prime initially).
  3. Use the sieve_of_eratosthenes function to mark non-prime numbers as 0 in the 'collection' array.
  4. Implement the is_primorial_prime function with the following steps: a. Check if 'num' is greater than 1 and if 'num' is a prime number (collection[num] == 1). b. If both conditions are met, initialize a variable 'product' to 1. c. Initialize a variable 'i' to 2 and start a loop until 'i' is less than or equal to 'num' and 'product' is less than or equal to 'num + 1'. d. Within the loop, if 'i' is a prime number (collection[i] == 1), do the following:
    • Check if 'product + 1' or 'product - 1' is equal to 'num'. If so, set 'status' to 1 (indicating 'num' is a Primorial Prime).
    • Otherwise, update 'product' by multiplying it with 'i'.
    • Increment 'i' by 1.
  5. Finally, in the main function, call the is_primorial_prime function with different 'num' values to check if they are Primorial Primes or not.

Code Solution

Here given code implementation process.

// C program
// Check if a number is Primorial Prime or not
#include <stdio.h>

#define SIZE 1000001
//Check whether number is Primorial Prime or not
void is_primorial_prime(int collection[], int num)
{
    //indicating the status of result prime number
    int status = 0;
    //Check that given number is greater than one and number is prime
    if (num > 1 && collection[num] == 1)
    {
        int product = 1;
        int i = 2;
        while (status == 0 && i <= num && product <= num + 1)
        {
            if (collection[i] == 1)
            {
                //When i is prime
                if (product + 1 == num || product - 1 == num)
                {
                    status = 1;
                }
                else
                {
                    product *= i;
                }
            }
            i++;
        }
    }
    if (status == 1)
    {
        printf("\n [%d] Is Primorial Prime", num);
    }
    else
    {
        printf("\n [%d] Is Not Primorial Prime", num);
    }
}
//Find all prime numbers under 1000001 
void sieve_of_eratosthenes(int collection[])
{
    // Loop controlling variables
    int i = 0;
    int j = 1;
    // Initial two numbers are not prime (0 and 1)
    collection[i] = 0;
    collection[j] = 0;
    // Set the initial (2 to n element is prime)
    for (i = 2; i < SIZE; ++i)
    {
        collection[i] = 1;
    }
    // Initial 0 and 1 are not prime
    // We start to 2
    for (i = 2; i * i < SIZE; ++i)
    {
        if (collection[i] == 1)
        {
            //When i is prime number
            //Modify the prime status of all next multiplier of location i
            for (j = i * i; j < SIZE; j += i)
            {
                collection[j] = 0;
            }
        }
    }
}
int main()
{
    //This is used to store prime number status
    int collection[SIZE];
    //Find find prime number
    sieve_of_eratosthenes(collection);
    //Test case
    is_primorial_prime(collection, 7);
    is_primorial_prime(collection, 107);
    is_primorial_prime(collection, 13);
    is_primorial_prime(collection, 1319);
    is_primorial_prime(collection, 211);
    is_primorial_prime(collection, 373);
    is_primorial_prime(collection, 29);
    return 0;
}

Output

 [7] Is Primorial Prime
 [107] Is Not Primorial Prime
 [13] Is Not Primorial Prime
 [1319] Is Not Primorial Prime
 [211] Is Primorial Prime
 [373] Is Not Primorial Prime
 [29] Is Primorial Prime
// Java program 
// Check if a number is primorial prime or not
class PrimorialPrime
{
    //Check whether number is Primorial Prime or not
    public void is_primorial_prime(boolean[] collection, int num)
    {
        //indicating the status of result prime number
        boolean status = false;
        //Check that given number is greater than one and number is prime
        if (num > 1 && collection[num] == true)
        {
            int product = 1;
            int i = 2;
            while (status == false && i <= num && product <= num + 1)
            {
                if (collection[i] == true)
                {
                    //When i is prime
                    if (product + 1 == num || product - 1 == num)
                    {
                        status = true;
                    }
                    else
                    {
                        product *= i;
                    }
                }
                i++;
            }
        }
        if (status == true)
        {
            System.out.print("\n [" + num + "] Is Primorial Prime");
        }
        else
        {
            System.out.print("\n [" + num + "] Is Not Primorial Prime");
        }
    }
    //Find all prime numbers under 1000001 
    public void sieve_of_eratosthenes(boolean[] collection, int size)
    {
        // Loop controlling variables
        int i = 0;
        int j = 1;
        // Initial two numbers are not prime (0 and 1)
        collection[i] = false;
        collection[j] = false;
        // Set the initial (2 to n element is prime)
        for (i = 2; i < size; ++i)
        {
            collection[i] = true;
        }
        // Initial 0 and 1 are not prime
        // We start to 2
        for (i = 2; i * i < size; ++i)
        {
            if (collection[i] == true)
            {
                //When i is prime number
                //Modify the prime status of all next multiplier of location i
                for (j = i * i; j < size; j += i)
                {
                    collection[j] = false;
                }
            }
        }
    }
    public static void main(String []args)
    {
        PrimorialPrime obj = new PrimorialPrime();
        int size = 1000001;
        //This is used to store prime number status
        boolean[] collection = new boolean[size];
        //Find find prime number
        obj.sieve_of_eratosthenes(collection, size);
        //Test case
        obj.is_primorial_prime(collection, 7);
        obj.is_primorial_prime(collection, 107);
        obj.is_primorial_prime(collection, 13);
        obj.is_primorial_prime(collection, 1319);
        obj.is_primorial_prime(collection, 211);
        obj.is_primorial_prime(collection, 373);
        obj.is_primorial_prime(collection, 29);
    }
}

Output

 [7] Is Primorial Prime
 [107] Is Not Primorial Prime
 [13] Is Not Primorial Prime
 [1319] Is Not Primorial Prime
 [211] Is Primorial Prime
 [373] Is Not Primorial Prime
 [29] Is Primorial Prime
//Include header file
#include <iostream>

using namespace std;
// C++ program 
// Check if a number is primorial prime or not
class PrimorialPrime
{
	public:
		//Check whether number is Primorial Prime or not
		void is_primorial_prime(bool collection[], int num)
		{
			//indicating the status of result prime number
			bool status = false;
			//Check that given number is greater than one and number is prime
			if (num > 1 && collection[num] == true)
			{
				int product = 1;
				int i = 2;
				while (status == false && i <= num && product <= num + 1)
				{
					if (collection[i] == true)
					{
						//When i is prime
						if (product + 1 == num || product - 1 == num)
						{
							status = true;
						}
						else
						{
							product *= i;
						}
					}
					i++;
				}
			}
			if (status == true)
			{
				cout << "\n [" << num << "] Is Primorial Prime";
			}
			else
			{
				cout << "\n [" << num << "] Is Not Primorial Prime";
			}
		}
	//Find all prime numbers under 1000001 
	void sieve_of_eratosthenes(bool collection[], int size)
	{
		// Loop controlling variables
		int i = 0;
		int j = 1;
		// Initial two numbers are not prime (0 and 1)
		collection[i] = false;
		collection[j] = false;
		// Set the initial (2 to n element is prime)
		for (i = 2; i < size; ++i)
		{
			collection[i] = true;
		}
		// Initial 0 and 1 are not prime
		// We start to 2
		for (i = 2; i *i < size; ++i)
		{
			if (collection[i] == true)
			{
				//When i is prime number
				//Modify the prime status of all next multiplier of location i
				for (j = i *i; j < size; j += i)
				{
					collection[j] = false;
				}
			}
		}
	}
};
int main()
{
	PrimorialPrime obj = PrimorialPrime();
	int size = 1000001;
	//This is used to store prime number status
	bool collection[size];
	//Find find prime number
	obj.sieve_of_eratosthenes(collection, size);
	//Test case
	obj.is_primorial_prime(collection, 7);
	obj.is_primorial_prime(collection, 107);
	obj.is_primorial_prime(collection, 13);
	obj.is_primorial_prime(collection, 1319);
	obj.is_primorial_prime(collection, 211);
	obj.is_primorial_prime(collection, 373);
	obj.is_primorial_prime(collection, 29);
	return 0;
}

Output

 [7] Is Primorial Prime
 [107] Is Not Primorial Prime
 [13] Is Not Primorial Prime
 [1319] Is Not Primorial Prime
 [211] Is Primorial Prime
 [373] Is Not Primorial Prime
 [29] Is Primorial Prime
//Include namespace system
using System;

// C# program 
// Check if a number is primorial prime or not

class PrimorialPrime
{
	//Check whether number is Primorial Prime or not
	public void is_primorial_prime(Boolean[] collection, int num)
	{
		//indicating the status of result prime number
		Boolean status = false;
		//Check that given number is greater than one and number is prime
		if (num > 1 && collection[num] == true)
		{
			int product = 1;
			int i = 2;
			while (status == false && i <= num && product <= num + 1)
			{
				if (collection[i] == true)
				{
					//When i is prime
					if (product + 1 == num || product - 1 == num)
					{
						status = true;
					}
					else
					{
						product *= i;
					}
				}
				i++;
			}
		}
		if (status == true)
		{
			Console.Write("\n [" + num + "] Is Primorial Prime");
		}
		else
		{
			Console.Write("\n [" + num + "] Is Not Primorial Prime");
		}
	}
	//Find all prime numbers under 1000001 
	public void sieve_of_eratosthenes(Boolean[] collection, int size)
	{
		// Loop controlling variables
		int i = 0;
		int j = 1;
		// Initial two numbers are not prime (0 and 1)
		collection[i] = false;
		collection[j] = false;
		// Set the initial (2 to n element is prime)
		for (i = 2; i < size; ++i)
		{
			collection[i] = true;
		}
		// Initial 0 and 1 are not prime
		// We start to 2
		for (i = 2; i * i < size; ++i)
		{
			if (collection[i] == true)
			{
				//When i is prime number
				//Modify the prime status of all next multiplier of location i
				for (j = i * i; j < size; j += i)
				{
					collection[j] = false;
				}
			}
		}
	}
	public static void Main(String[] args)
	{
		PrimorialPrime obj = new PrimorialPrime();
		int size = 1000001;
		//This is used to store prime number status
		Boolean[] collection = new Boolean[size];
		//Find find prime number
		obj.sieve_of_eratosthenes(collection, size);
		//Test case
		obj.is_primorial_prime(collection, 7);
		obj.is_primorial_prime(collection, 107);
		obj.is_primorial_prime(collection, 13);
		obj.is_primorial_prime(collection, 1319);
		obj.is_primorial_prime(collection, 211);
		obj.is_primorial_prime(collection, 373);
		obj.is_primorial_prime(collection, 29);
	}
}

Output

 [7] Is Primorial Prime
 [107] Is Not Primorial Prime
 [13] Is Not Primorial Prime
 [1319] Is Not Primorial Prime
 [211] Is Primorial Prime
 [373] Is Not Primorial Prime
 [29] Is Primorial Prime
<?php
// Php porgram 
// Check if a number is primorial prime or not
class PrimorialPrime
{
    //Check whether number is Primorial Prime or not
    public  function is_primorial_prime( & $collection, $num)
    {
        //indicating the status of result prime number
        $status = false;
        //Check that given number is greater than one and number is prime
        if ($num > 1 && $collection[$num] == true)
        {
            $product = 1;
            $i = 2;
            while ($status == false && $i <= $num && $product <= $num + 1)
            {
                if ($collection[$i] == true)
                {
                    //When i is prime
                    if ($product + 1 == $num || $product - 1 == $num)
                    {
                        $status = true;
                    }
                    else
                    {
                        $product *= $i;
                    }
                }
                $i++;
            }
        }
        if ($status == true)
        {
            echo "\n [". $num ."] Is Primorial Prime";
        }
        else
        {
            echo "\n [". $num ."] Is Not Primorial Prime";
        }
    }
    //Find all prime numbers under 1000001 
    public  function sieve_of_eratosthenes( & $collection, $size)
    {
        // Loop controlling variables
        $i = 0;
        $j = 1;
        // Initial two numbers are not prime (0 and 1)
        $collection[$i] = false;
        $collection[$j] = false;
        // Initial 0 and 1 are not prime
        // We start to 2
        for ($i = 2; $i * $i < $size; ++$i)
        {
            if ($collection[$i] == true)
            {
                //When i is prime number
                //Modify the prime status of all next multiplier of location i
                for ($j = $i * $i; $j < $size; $j += $i)
                {
                    $collection[$j] = false;
                }
            }
        }
    }
}

function main()
{
    $obj = new PrimorialPrime();
    $size = 1000001;
    //This is used to store prime number status
    $collection = array_fill(0, $size, true);
    //Find find prime number
    $obj->sieve_of_eratosthenes($collection, $size);
    //Test case
    $obj->is_primorial_prime($collection, 7);
    $obj->is_primorial_prime($collection, 107);
    $obj->is_primorial_prime($collection, 13);
    $obj->is_primorial_prime($collection, 1319);
    $obj->is_primorial_prime($collection, 211);
    $obj->is_primorial_prime($collection, 373);
    $obj->is_primorial_prime($collection, 29);
}
main();

Output

 [7] Is Primorial Prime
 [107] Is Not Primorial Prime
 [13] Is Not Primorial Prime
 [1319] Is Not Primorial Prime
 [211] Is Primorial Prime
 [373] Is Not Primorial Prime
 [29] Is Primorial Prime
// Node Js program 
// Check if a number is primorial prime or not
class PrimorialPrime
{
	//Check whether number is Primorial Prime or not
	is_primorial_prime(collection, num)
	{
		//indicating the status of result prime number
		var status = false;
		//Check that given number is greater than one and number is prime
		if (num > 1 && collection[num] == true)
		{
			var product = 1;
			var i = 2;
			while (status == false && i <= num && product <= num + 1)
			{
				if (collection[i] == true)
				{
					//When i is prime
					if (product + 1 == num || product - 1 == num)
					{
						status = true;
					}
					else
					{
						product *= i;
					}
				}
				i++;
			}
		}
		if (status == true)
		{
			process.stdout.write("\n [" + num + "] Is Primorial Prime");
		}
		else
		{
			process.stdout.write("\n [" + num + "] Is Not Primorial Prime");
		}
	}
	//Find all prime numbers under 1000001 
	sieve_of_eratosthenes(collection, size)
	{
		// Loop controlling variables
		var i = 0;
		var j = 1;
		// Initial two numbers are not prime (0 and 1)
		collection[i] = false;
		collection[j] = false;
		// Set the initial (2 to n element is prime)
		for (i = 2; i < size; ++i)
		{
			collection[i] = true;
		}
		// Initial 0 and 1 are not prime
		// We start to 2
		for (i = 2; i * i < size; ++i)
		{
			if (collection[i] == true)
			{
				//When i is prime number
				//Modify the prime status of all next multiplier of location i
				for (j = i * i; j < size; j += i)
				{
					collection[j] = false;
				}
			}
		}
	}
}

function main()
{
	var obj = new PrimorialPrime();
	var size = 1000001;
	//This is used to store prime number status
	var collection = Array(size).fill(false);
	//Find find prime number
	obj.sieve_of_eratosthenes(collection, size);
	//Test case
	obj.is_primorial_prime(collection, 7);
	obj.is_primorial_prime(collection, 107);
	obj.is_primorial_prime(collection, 13);
	obj.is_primorial_prime(collection, 1319);
	obj.is_primorial_prime(collection, 211);
	obj.is_primorial_prime(collection, 373);
	obj.is_primorial_prime(collection, 29);
}
main();

Output

 [7] Is Primorial Prime
 [107] Is Not Primorial Prime
 [13] Is Not Primorial Prime
 [1319] Is Not Primorial Prime
 [211] Is Primorial Prime
 [373] Is Not Primorial Prime
 [29] Is Primorial Prime
#  Python 3 program 
#  Check if a number is primorial prime or not
class PrimorialPrime :
	# Check whether number is Primorial Prime or not
	def is_primorial_prime(self, collection, num) :
		# indicating the status of result prime number
		status = False
		# Check that given number is greater than one and number is prime
		if (num > 1 and collection[num] == True) :
			product = 1
			i = 2
			while (status == False and i <= num and product <= num + 1) :
				if (collection[i] == True) :
					# When i is prime
					if (product + 1 == num or product - 1 == num) :
						status = True
					else :
						product *= i
					
				
				i += 1
			
		
		if (status == True) :
			print("\n [{0}] Is Primorial Prime".format(num), end = "")
		else :
			print("\n [{0}] Is Not Primorial Prime".format(num), end = "")
		
	
	# Find all prime numbers under 1000001 
	def sieve_of_eratosthenes(self, collection, size) :
		#  Loop controlling variables
		i = 0
		j = 1
		#  Initial two numbers are not prime (0 and 1)
		collection[i] = False
		collection[j] = False
		#  Initial 0 and 1 are not prime
		#  We start to 2
		i = 2
		while (i * i < size) :
			if (collection[i] == True) :
				# When i is prime number
				# Modify the prime status of all next multiplier of location i
				j = i * i
				while (j < size) :
					collection[j] = False
					j += i
				
			
			i += 1
		
	

def main() :
	obj = PrimorialPrime()
	size = 1000001
	# This is used to store prime number status
	collection = [True] * (size)
	# Find find prime number
	obj.sieve_of_eratosthenes(collection, size)
	# Test case
	obj.is_primorial_prime(collection, 7)
	obj.is_primorial_prime(collection, 107)
	obj.is_primorial_prime(collection, 13)
	obj.is_primorial_prime(collection, 1319)
	obj.is_primorial_prime(collection, 211)
	obj.is_primorial_prime(collection, 373)
	obj.is_primorial_prime(collection, 29)

if __name__ == "__main__": main()

Output

 [7] Is Primorial Prime
 [107] Is Not Primorial Prime
 [13] Is Not Primorial Prime
 [1319] Is Not Primorial Prime
 [211] Is Primorial Prime
 [373] Is Not Primorial Prime
 [29] Is Primorial Prime
#  Ruby program 
#  Check if a number is primorial prime or not
class PrimorialPrime 
	# Check whether number is Primorial Prime or not
	def is_primorial_prime(collection, num) 
		# indicating the status of result prime number
		status = false
		# Check that given number is greater than one and number is prime
		if (num > 1 && collection[num] == true) 
			product = 1
			i = 2
			while (status == false && i <= num && product <= num + 1) 
				if (collection[i] == true) 
					# When i is prime
					if (product + 1 == num || product - 1 == num) 
						status = true
					else 
						product *= i
					end

				end

				i += 1
			end

		end

		if (status == true) 
			print("\n [", num ,"] Is Primorial Prime")
		else 
			print("\n [", num ,"] Is Not Primorial Prime")
		end

	end

	# Find all prime numbers under 1000001 
	def sieve_of_eratosthenes(collection, size) 
		#  Loop controlling variables
		i = 0
		j = 1
		#  Initial two numbers are not prime (0 and 1)
		collection[i] = false
		collection[j] = false
		#  Initial 0 and 1 are not prime
		#  We start to 2
		i = 2
		while (i * i < size) 
			if (collection[i] == true) 
				# When i is prime number
				# Modify the prime status of all next multiplier of location i
				j = i * i
				while (j < size) 
					collection[j] = false
					j += i
				end

			end

			i += 1
		end

	end

end

def main() 
	obj = PrimorialPrime.new()
	size = 1000001
	# This is used to store prime number status
	collection = Array.new(size) {true}
	# Find find prime number
	obj.sieve_of_eratosthenes(collection, size)
	# Test case
	obj.is_primorial_prime(collection, 7)
	obj.is_primorial_prime(collection, 107)
	obj.is_primorial_prime(collection, 13)
	obj.is_primorial_prime(collection, 1319)
	obj.is_primorial_prime(collection, 211)
	obj.is_primorial_prime(collection, 373)
	obj.is_primorial_prime(collection, 29)
end

main()

Output

 [7] Is Primorial Prime
 [107] Is Not Primorial Prime
 [13] Is Not Primorial Prime
 [1319] Is Not Primorial Prime
 [211] Is Primorial Prime
 [373] Is Not Primorial Prime
 [29] Is Primorial Prime
// Scala program 
// Check if a number is primorial prime or not
class PrimorialPrime
{
	//Check whether number is Primorial Prime or not
	def is_primorial_prime(collection: Array[Boolean], num: Int): Unit = {
		//indicating the status of result prime number
		var status: Boolean = false;
		//Check that given number is greater than one and number is prime
		if (num > 1 && collection(num) == true)
		{
			var product: Int = 1;
			var i: Int = 2;
			while (status == false && i <= num && product <= num + 1)
			{
				if (collection(i) == true)
				{
					//When i is prime
					if (product + 1 == num || product - 1 == num)
					{
						status = true;
					}
					else
					{
						product *= i;
					}
				}
				i += 1;
			}
		}
		if (status == true)
		{
			print("\n [" + num + "] Is Primorial Prime");
		}
		else
		{
			print("\n [" + num + "] Is Not Primorial Prime");
		}
	}
	//Find all prime numbers under 1000001 
	def sieve_of_eratosthenes(collection: Array[Boolean], size: Int): Unit = {
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 1;
		// Initial two numbers are not prime (0 and 1)
		collection(i) = false;
		collection(j) = false;
		// Initial 0 and 1 are not prime
		// We start to 2
		i = 2;
		while (i * i < size)
		{
			if (collection(i) == true)
			{
				//When i is prime number
				//Modify the prime status of all next multiplier of location i
				j = i * i;
				while (j < size)
				{
					collection(j) = false;
					j += i;
				}
			}
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: PrimorialPrime = new PrimorialPrime();
		var size: Int = 1000001;
		//This is used to store prime number status
		var collection: Array[Boolean] = Array.fill[Boolean](size)(true);
		//Find find prime number
		obj.sieve_of_eratosthenes(collection, size);
		//Test case
		obj.is_primorial_prime(collection, 7);
		obj.is_primorial_prime(collection, 107);
		obj.is_primorial_prime(collection, 13);
		obj.is_primorial_prime(collection, 1319);
		obj.is_primorial_prime(collection, 211);
		obj.is_primorial_prime(collection, 373);
		obj.is_primorial_prime(collection, 29);
	}
}

Output

 [7] Is Primorial Prime
 [107] Is Not Primorial Prime
 [13] Is Not Primorial Prime
 [1319] Is Not Primorial Prime
 [211] Is Primorial Prime
 [373] Is Not Primorial Prime
 [29] Is Primorial Prime
// Swift 4 program 
// Check if a number is primorial prime or not
class PrimorialPrime
{
	//Check whether number is Primorial Prime or not
	func is_primorial_prime(_ collection: [Bool], _ num: Int)
	{
		//indicating the status of result prime number
		var status: Bool = false;
		//Check that given number is greater than one and number is prime
		if (num > 1 && collection[num] == true)
		{
			var product: Int = 1;
			var i: Int = 2;
			while (status == false && i <= num && product <= num + 1)
			{
				if (collection[i] == true)
				{
					//When i is prime
					if (product + 1 == num || product - 1 == num)
					{
						status = true;
					}
					else
					{
						product *= i;
					}
				}
				i += 1;
			}
		}
		if (status == true)
		{
			print("\n [\(num)] Is Primorial Prime", terminator: "");
		}
		else
		{
			print("\n [\(num)] Is Not Primorial Prime", terminator: "");
		}
	}
	//Find all prime numbers under 1000001 
	func sieve_of_eratosthenes(_ collection: inout[Bool], _ size: Int)
	{
		// Loop controlling variables
		var i: Int = 0;
		var j: Int = 1;
		// Initial two numbers are not prime (0 and 1)
		collection[i] = false;
		collection[j] = false;
		// Initial 0 and 1 are not prime
		// We start to 2
		i = 2;
		while (i * i < size)
		{
			if (collection[i] == true)
			{
				//When i is prime number
				//Modify the prime status of all next multiplier of location i
				j = i * i;
				while (j < size)
				{
					collection[j] = false;
					j += i;
				}
			}
			i += 1;
		}
	}
}
func main()
{
	let obj: PrimorialPrime = PrimorialPrime();
	let size: Int = 1000001;
	//This is used to store prime number status
	var collection: [Bool] = Array(repeating: true, count: size);
	//Find find prime number
	obj.sieve_of_eratosthenes(&collection, size);
	//Test case
	obj.is_primorial_prime(collection, 7);
	obj.is_primorial_prime(collection, 107);
	obj.is_primorial_prime(collection, 13);
	obj.is_primorial_prime(collection, 1319);
	obj.is_primorial_prime(collection, 211);
	obj.is_primorial_prime(collection, 373);
	obj.is_primorial_prime(collection, 29);
}
main();

Output

 [7] Is Primorial Prime
 [107] Is Not Primorial Prime
 [13] Is Not Primorial Prime
 [1319] Is Not Primorial Prime
 [211] Is Primorial Prime
 [373] Is Not Primorial Prime
 [29] Is Primorial Prime

Resultant Output Explanation

The given code will output whether the provided numbers are Primorial Primes or not. Based on the example test cases provided in the code, the output will be as follows:

  • 7 is a Primorial Prime.
  • 107 is not a Primorial Prime.
  • 13 is not a Primorial Prime.
  • 1319 is not a Primorial Prime.
  • 211 is a Primorial Prime.
  • 373 is not a Primorial Prime.
  • 29 is a Primorial Prime.

Time Complexity

The time complexity of finding all prime numbers up to SIZE using the Sieve of Eratosthenes algorithm is O(n log log n). The is_primorial_prime function loops through the prime numbers up to 'num', which takes O(n) time. Therefore, the overall time complexity of the code is O(n log log n). Here, 'n' represents the given number 'num'.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment