Given a positive integer N, calculate the product of the first N prime numbers.
Input : N = 3 Output : 30 Explanation : First 3 prime numbers are 2, 3, 5. Input : N = 5 Output : 2310
- Create a sieve which will help us to identify if the number is prime or not in O(1) time.
- Run a loop starting from 1 until and unless we find n prime numbers.
- Multiply all the prime numbers and neglect those which are not prime.
- Then, display the product of 1st N prime numbers.
Time Complexity – O( Nlog(logN) )
Below is the implementation of above approach:
NOTE: For larger values of N, the product may be give integer overflow errors.
Also for multiple queries, prefix array technique can be used which will give output of each query in O(1) after making the prefix array first which will take O(N) time.
- Find product of prime numbers between 1 to n
- 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 two numbers with sum and product both same as N
- Find the XOR of first N Prime Numbers
- Find two prime numbers with given sum
- Find if n can be written as product of k numbers
- 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
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.