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++
// 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)
{
// Multiply first n primes
int result = 1;
for (int i=0; i<n; i++)
result = result * primes[i];
return result;
}
// Driver code
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
// Java program to find Primorial of given numbers
import java.util.*;
class GFG{
public static int MAX = 1000000;
// vector to store all prime less than and equal to 10^6
static ArrayList<Integer> primes = new ArrayList<Integer>();
// Function for sieve of sundaram. This function stores all
// prime numbers less than MAX in primes
static 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
boolean[] marked = new boolean[MAX];
// Main logic of Sundaram. Mark all numbers which
// do not generate prime number by doing 2*i+1
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;
}
}
// Since 2 is a prime number
primes.add(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.add(2 * i + 1);
}
}
}
// Function to calculate primorial of n
static int calculatePrimorial(int n)
{
// Multiply first n primes
int result = 1;
for (int i = 0; i < n; i++)
{
result = result * primes.get(i);
}
return result;
}
// Driver code
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));
}
}
}
// This Code is contributed by mits
Python3
# Python3 program to find Primorial of given numbers
import math
MAX = 1000000;
# vector to store all prime less than and equal to 10^6
primes=[];
# Function for sieve of sundaram. This function stores all
# prime numbers less than MAX in primes
def 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
marked=[False]*(int(MAX/2)+1);
# Main logic of Sundaram. Mark all numbers which
# do not generate prime number by doing 2*i+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;
# Since 2 is a prime number
primes.append(2);
# Print other primes. Remaining primes are of the
# form 2*i + 1 such that marked[i] is false.
for i in range(1,int(MAX/2)):
if (marked[i] == False):
primes.append(2*i + 1);
# Function to calculate primorial of n
def calculatePrimorial(n):
# Multiply first n primes
result = 1;
for i in range(n):
result = result * primes[i];
return result;
# Driver code
n = 5;
sieveSundaram();
for i in range(1,n+1):
print("Primorial(P#) of",i,"is",calculatePrimorial(i));
# This code is contributed by mits
C#
// C# program to find Primorial of given numbers
using System;
using System.Collections;
class GFG{
public static int MAX = 1000000;
// vector to store all prime less than and equal to 10^6
static ArrayList primes = new ArrayList();
// Function for sieve of sundaram. This function stores all
// prime numbers less than MAX in primes
static 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 = new bool[MAX];
// Main logic of Sundaram. Mark all numbers which
// do not generate prime number by doing 2*i+1
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;
}
}
// Since 2 is a prime number
primes.Add(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.Add(2 * i + 1);
}
}
}
// Function to calculate primorial of n
static int calculatePrimorial(int n)
{
// Multiply first n primes
int result = 1;
for (int i = 0; i < n; i++)
{
result = result * (int)primes[i];
}
return result;
}
// Driver code
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));
}
}
}
// This Code is contributed by mits
PHP
<?php
// PHP program to find Primorial
// of given numbers
$MAX = 100000;
// vector to store all prime less
// than and equal to 10^6
$primes = array();
// Function for sieve of sundaram.
// This function stores all prime
// numbers less than MAX in primes
function sieveSundaram()
{
global $MAX, $primes;
// 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
$marked = array_fill(0, $MAX / 2 + 1, 0);
// Main logic of Sundaram. Mark all numbers which
// do not generate prime number by doing 2*i+1
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;
// Since 2 is a prime number
array_push($primes, 2);
// Print other primes. Remaining primes
// are of the form 2*i + 1 such that
// marked[i] is false.
for ($i = 1; $i <= $MAX / 2; $i++)
if ($marked[$i] == false)
array_push($primes, (2 * $i + 1));
}
// Function to calculate primorial of n
function calculatePrimorial($n)
{
global $primes;
// Multiply first n primes
$result = 1;
for ($i = 0; $i < $n; $i++)
$result = $result * $primes[$i];
return $result;
}
// Driver code
$n = 5;
sieveSundaram();
for ($i = 1 ; $i<= $n; $i++)
echo "Primorial(P#) of " . $i .
" is " . calculatePrimorial($i) . "\n";
// This code is contributed by mits
?>
JavaScript
<script>
// Javascript program to find Primorial
// of given numbers
let MAX = 100000;
// vector to store all prime less
// than and equal to 10^6
let primes = new Array();
// Function for sieve of sundaram.
// This function stores all prime
// numbers less than MAX in primes
function 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
let marked = new Array(MAX / 2 + 1).fill(0);
// Main logic of Sundaram. Mark all numbers which
// do not generate prime number by doing 2*i+1
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;
// Since 2 is a prime number
primes.push(2);
// Print other primes. Remaining primes
// are of the form 2*i + 1 such that
// marked[i] is false.
for (let i = 1; i <= MAX / 2; i++)
if (marked[i] == false)
primes.push(2 * i + 1);
}
// Function to calculate primorial of n
function calculatePrimorial(n)
{
// Multiply first n primes
let result = 1;
for (let i = 0; i < n; i++)
result = result * primes[i];
return result;
}
// Driver code
let n = 5;
sieveSundaram();
for (let i = 1 ; i<= n; i++)
document.write("Primorial(P#) of " + i + " is " +
calculatePrimorial(i) + "<br>");
// This code is contributed by gfgking
</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)
Similar Reads
Factorial of a Number Given the number n (n >=0), find its factorial. Factorial of n is defined as 1 x 2 x ... x n. For n = 0, factorial is 1. We are going to discuss iterative and recursive programs in this post.Examples:Input: n = 5Output: 120Explanation: 5! = 5 * 4 * 3 * 2 * 1 = 120Input: n = 4Output: 24Explanation
7 min read
Legendre's formula - Largest power of a prime p in n! Given an integer n and a prime number p, the task is to find the largest x such that px (p raised to power x) divides n!.Examples: Input: n = 7, p = 3Output: x = 2Explanation: 32 divides 7! and 2 is the largest such power of 3.Input: n = 10, p = 3Output: x = 4Explanation: 34 divides 10! and 4 is the
6 min read
Count trailing zeroes in factorial of a number Given an integer n, write a function that returns count of trailing zeroes in n!. Examples : Input: n = 5Output: 1 Explanation: Factorial of 5 is 120 which has one trailing 0.Input: n = 20Output: 4Explanation: Factorial of 20 is 2432902008176640000 which has 4 trailing zeroes.Input: n = 100Output: 2
8 min read
Find the Factorial of a large number Factorial of a non-negative integer, is the multiplication of all integers smaller than or equal to n. Factorial of a numberExamples: Input: 100Output: 933262154439441526816992388562667004- 907159682643816214685929638952175999- 932299156089414639761565182862536979- 2082722375825118521091686400000000
15+ min read
Primorial of a number 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 primo
10 min read
Find maximum power of a number that divides a factorial Given two numbers, fact and n, find the largest power of n that divides fact! (Factorial of fact). Examples:Â Input : fact = 5, n = 2 Output : 3 Explanation: Value of 5! is 120. The largest power of 2 that divides 120 is 8 (or 23 Input : fact = 146, n = 15 Output : 35 The idea is based on Legendreâs
12 min read
Largest power of k in n! (factorial) where k may not be prime Given two positive integers k and n, where k > 1, find the largest power of k that divides n! (n factorial).Examples: Input: n = 7, k = 2Output: 4Explanation: 7! = 5040, and 24 = 16 is the highest power of 2 that divides 5040.Input: n = 10, k = 9Output: 2Explanation: 10! = 3628800, and 9² = 81 is
15+ min read
Check if a number is a Krishnamurthy Number or not A Krishnamurthy number is a number whose sum of the factorial of digits is equal to the number itself.For example, 145 is the sum of the factorial of each digit.1! + 4! + 5! = 1 + 24 + 120 = 145 Examples: Input : 145Output : YESExplanation: 1! + 4! + 5! = 1 + 24 + 120 = 145, which is equal to input,
10 min read
Last non-zero digit of a factorial Given a number n, find the last non-zero digit in n!.Examples: Input : n = 5 Output : 2 5! = 5 * 4 * 3 * 2 * 1 = 120 Last non-zero digit in 120 is 2. Input : n = 33 Output : 8 Recommended PracticeLast non-zero digit in factorialTry It! A Simple Solution is to first find n!, then find the last non-ze
14 min read
Count digits in a factorial using Logarithm Given an integer N, find the number of digits that appear in its factorial, where factorial is defined as, factorial(n) = 1*2*3*4........*n and factorial(0) = 1Examples : Input: 5Output: 3Explanation: 5! = 120, i.e., 3 digitsInput: 10Output: 7Explanation: 10! = 3628800, i.e., 7 digits Naive approach
8 min read