Primorial of a number

Given a number n, the task is to calculate its primorial. Primorial of a number is similar to factorial of a number. In primorial, not all the natural numbers get multiplied only prime numbers are multiplied to calculate the primorial of a number. It is denoted with P#.

Examples:

```Input: n = 5
Output: 30
Priomorial = 2 * 3 * 5 = 30
As a side note, factorial is 2 * 3 * 4 * 5

Input: n = 12
Output: 2310
Primorial = 2 * 3 * 5 * 7 * 11
```

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A naive approach is to check all numbers from 1 to n one by one is prime or not, if yes then store the multiplication in result, similarly store the result of multiplication of primes till n.

An efficient method is to find all the prime up-to n using Sieve of Sundaram and then just calculate the primorial by multiplying them all.

```// C++ program to find Primorial of given numbers
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1000000;

// vector to store all prime less than and equal to 10^6
vector <int> primes;

// Function for sieve of sundaram. This function stores all
// prime numbers less than MAX in primes
void sieveSundaram()
{
// In general Sieve of Sundaram, produces primes smaller
// than (2*x + 2) for a number given number x. Since
// we want primes smaller than MAX, we reduce MAX to half
// This array is used to separate numbers of the form
// i+j+2ij from others where 1 <= i <= j
bool marked[MAX/2 + 1] = {0};

// Main logic of Sundaram. Mark all numbers which
// do not generate prime number by doing 2*i+1
for (int i = 1; i <= (sqrt(MAX)-1)/2 ; i++)
for (int j = (i*(i+1))<<1 ; j <= MAX/2 ; j += 2*i +1)
marked[j] = true;

// Since 2 is a prime number
primes.push_back(2);

// Print other primes. Remaining primes are of the
// form 2*i + 1 such that marked[i] is false.
for (int i=1; i<=MAX/2; i++)
if (marked[i] == false)
primes.push_back(2*i + 1);
}

// Function to calculate primorial of n
int calculatePrimorial(int n)
{
int result = 1;  // Initialize result

// Multiply all primes up-to n and store in result
for (int i = 0; primes[i] <= n ; i++)
result = result * primes[i];

return result;
}

// Driver code
int main()
{
int n = 15;
sieveSundaram();
for (int i = 1 ; i<= n; i++)
cout << "Primorial(P#) of " << i << " is "
<< calculatePrimorial(i) <<endl;
return 0;
}
```

Output:

```Primorial(P#) of 1 is 1
Primorial(P#) of 2 is 2
Primorial(P#) of 3 is 6
Primorial(P#) of 4 is 6
Primorial(P#) of 5 is 30
Primorial(P#) of 6 is 30
Primorial(P#) of 7 is 210
Primorial(P#) of 8 is 210
Primorial(P#) of 9 is 210
Primorial(P#) of 10 is 210
Primorial(P#) of 11 is 2310
Primorial(P#) of 12 is 2310
Primorial(P#) of 13 is 30030
Primorial(P#) of 14 is 30030
Primorial(P#) of 15 is 30030
```

This article is contributed by Sahil Chhabra (KILLER). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
0 Average Difficulty : 0/5.0