VOOZH about

URL: https://www.geeksforgeeks.org/dsa/primorial-of-a-number/

⇱ Primorial of a number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Primorial of a number

Last Updated : 11 Sep, 2023

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 


 

Recommended Practice


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.
 

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) 


 

Comment
Article Tags:
Article Tags: