Given a number n, the task is to calculate its primorial. Primorial (denoted as Pn#) is a product of first n prime numbers. Primorial of a number is similar to the 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 = 3
Output: 30
Primorial = 2 * 3 * 5 = 30
As a side note, factorial is 2 * 3 * 4 * 5
Input: n = 5
Output: 2310
Primorial = 2 * 3 * 5 * 7 * 11
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++
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1000000;
vector < int > primes;
void sieveSundaram()
{
bool marked[MAX/2 + 1] = {0};
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 ;
primes.push_back(2);
for ( int i=1; i<=MAX/2; i++)
if (marked[i] == false )
primes.push_back(2*i + 1);
}
int calculatePrimorial( int n)
{
int result = 1;
for ( int i=0; i<n; i++)
result = result * primes[i];
return result;
}
int main()
{
int n = 5;
sieveSundaram();
for ( int i = 1 ; i<= n; i++)
cout << "Primorial(P#) of " << i << " is "
<< calculatePrimorial(i) <<endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int MAX = 1000000 ;
static ArrayList<Integer> primes = new ArrayList<Integer>();
static void sieveSundaram()
{
boolean [] marked = new boolean [MAX];
for ( int i = 1 ; i <= (Math.sqrt(MAX) - 1 ) / 2 ; i++)
{
for ( int j = (i * (i + 1 )) << 1 ; j <= MAX / 2 ; j += 2 * i + 1 )
{
marked[j] = true ;
}
}
primes.add( 2 );
for ( int i = 1 ; i <= MAX / 2 ; i++)
{
if (marked[i] == false )
{
primes.add( 2 * i + 1 );
}
}
}
static int calculatePrimorial( int n)
{
int result = 1 ;
for ( int i = 0 ; i < n; i++)
{
result = result * primes.get(i);
}
return result;
}
public static void main(String[] args)
{
int n = 5 ;
sieveSundaram();
for ( int i = 1 ; i <= n; i++)
{
System.out.println( "Primorial(P#) of " +i+ " is " +calculatePrimorial(i));
}
}
}
|
Python3
import math
MAX = 1000000 ;
primes = [];
def sieveSundaram():
marked = [ False ] * ( int ( MAX / 2 ) + 1 );
for i in range ( 1 , int ((math.sqrt( MAX ) - 1 ) / 2 ) + 1 ):
for j in range (((i * (i + 1 ))<< 1 ),( int ( MAX / 2 ) + 1 ),( 2 * i + 1 )):
marked[j] = True ;
primes.append( 2 );
for i in range ( 1 , int ( MAX / 2 )):
if (marked[i] = = False ):
primes.append( 2 * i + 1 );
def calculatePrimorial(n):
result = 1 ;
for i in range (n):
result = result * primes[i];
return result;
n = 5 ;
sieveSundaram();
for i in range ( 1 ,n + 1 ):
print ( "Primorial(P#) of" ,i, "is" ,calculatePrimorial(i));
|
C#
using System;
using System.Collections;
class GFG{
public static int MAX = 1000000;
static ArrayList primes = new ArrayList();
static void sieveSundaram()
{
bool [] marked = new bool [MAX];
for ( int i = 1; i <= (Math.Sqrt(MAX) - 1) / 2 ; i++)
{
for ( int j = (i * (i + 1)) << 1 ; j <= MAX / 2 ; j += 2 * i + 1)
{
marked[j] = true ;
}
}
primes.Add(2);
for ( int i = 1; i <= MAX / 2; i++)
{
if (marked[i] == false )
{
primes.Add(2 * i + 1);
}
}
}
static int calculatePrimorial( int n)
{
int result = 1;
for ( int i = 0; i < n; i++)
{
result = result * ( int )primes[i];
}
return result;
}
public static void Main()
{
int n = 5;
sieveSundaram();
for ( int i = 1 ; i <= n; i++)
{
System.Console.WriteLine( "Primorial(P#) of " +i+ " is " +calculatePrimorial(i));
}
}
}
|
PHP
<?php
$MAX = 100000;
$primes = array ();
function sieveSundaram()
{
global $MAX , $primes ;
$marked = array_fill (0, $MAX / 2 + 1, 0);
for ( $i = 1; $i <= (sqrt( $MAX ) - 1) / 2 ; $i ++)
for ( $j = ( $i * ( $i + 1)) << 1 ;
$j <= $MAX / 2 ; $j += 2 * $i + 1)
$marked [ $j ] = true;
array_push ( $primes , 2);
for ( $i = 1; $i <= $MAX / 2; $i ++)
if ( $marked [ $i ] == false)
array_push ( $primes , (2 * $i + 1));
}
function calculatePrimorial( $n )
{
global $primes ;
$result = 1;
for ( $i = 0; $i < $n ; $i ++)
$result = $result * $primes [ $i ];
return $result ;
}
$n = 5;
sieveSundaram();
for ( $i = 1 ; $i <= $n ; $i ++)
echo "Primorial(P#) of " . $i .
" is " . calculatePrimorial( $i ) . "\n" ;
?>
|
Javascript
<script>
let MAX = 100000;
let primes = new Array();
function sieveSundaram()
{
let marked = new Array(MAX / 2 + 1).fill(0);
for (let i = 1; i <= (Math.sqrt(MAX) - 1) / 2 ; i++)
for (let j = (i * (i + 1)) << 1 ;
j <= MAX / 2 ; j += 2 * i + 1)
marked[j] = true ;
primes.push(2);
for (let i = 1; i <= MAX / 2; i++)
if (marked[i] == false )
primes.push(2 * i + 1);
}
function calculatePrimorial(n)
{
let result = 1;
for (let i = 0; i < n; i++)
result = result * primes[i];
return result;
}
let n = 5;
sieveSundaram();
for (let i = 1 ; i<= n; i++)
document.write( "Primorial(P#) of " + i + " is " +
calculatePrimorial(i) + "<br>" );
</script>
|
Output:
Primorial(P#) of 1 is 2
Primorial(P#) of 2 is 6
Primorial(P#) of 3 is 30
Primorial(P#) of 4 is 210
Primorial(P#) of 5 is 2310
Time complexity :- O(N)
This article is contributed by Sahil Chhabra . If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.