Given a number n, the task is to calculate its primorial. Primorial (denoted as Pn#) is product of first n prime numbers. 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#.
Input: n = 3 Output: 30 Priomorial = 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.
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
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 firstname.lastname@example.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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a number is Primorial Prime or not
- Number of factors of very large number N modulo M where M is any prime number
- Find minimum number to be divided to make a number a perfect square
- Super Ugly Number (Number whose prime factors are in given set)
- Number with maximum number of prime factors
- Determine whether a given number is a Hyperperfect Number
- Check if a number is a Krishnamurthy Number or not
- Total number of divisors for a given number
- Check if a number is divisible by all prime divisors of another number
- Largest number less than N whose each digit is prime number
- Check if given number is Emirp Number or not
- k-Rough Number or k-Jagged Number
- Queries on sum of odd number digit sums of all the factors of a number
- Number which has the maximum number of distinct prime factors in the range M to N
- First triangular number whose number of divisors exceeds N
- Find the largest good number in the divisors of given number N
- Check whether the given number is Euclid Number or not
- Represent a number as a sum of maximum possible number of Prime Numbers
- Check if a number is an Unusual Number or not
- Check whether a number is Non-hypotenuse number
Improved By : Mithun Kumar