Given a number n, we need to find the product of all prime numbers between 1 to n.
Input: 5 Output: 30 Explanation: product of prime numbers between 1 to 5 is 2 * 3 * 5 = 30 Input : 7 Output : 210
Using Sieve of Eratosthenes to find all prime numbers from 1 to n then compute the product.
Following is the algorithm to find all the prime numbers less than or equal to a given integer n by Eratosthenes’ method:
- Create a list of consecutive integers from 2 to n: (2, 3, 4, …, n).
- Initially, let p equal 2, the first prime number.
- Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked.
- Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.
When the algorithm terminates, all the numbers in the list that are not marked are prime and using a loop we compute the product of prime numbers.
- Find the Product of first N Prime Numbers
- Find two distinct prime numbers with given product
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Numbers less than N which are product of exactly two distinct prime numbers
- Product of all prime numbers in an Array
- Check if each element of the given array is the product of exactly K prime numbers
- Sum and product of k smallest and k largest prime numbers in the array
- Check if product of array containing prime numbers is a perfect square
- Find the XOR of first N Prime Numbers
- Find two prime numbers with given sum
- Find the sum of prime numbers in the Kth array
- Find count of Almost Prime numbers from 1 to N
- Find out the prime numbers in the form of A+nB or B+nA
- Program to find sum of prime numbers between 1 to n
- Program to find Prime Numbers Between given Interval
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.