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.
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.
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Permutation of first N positive integers such that prime numbers are at prime indices
- Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
- Numbers less than N which are product of exactly two distinct prime numbers
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Find product of prime numbers between 1 to n
- Find two distinct prime numbers with given product
- Find prime numbers in the first half and second half of an array
- Find the XOR of first N Prime Numbers
- Product of all prime numbers in an Array
- 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
- Check if each element of the given array is the product of exactly K prime numbers
- Product of all numbers up to N that are co-prime with N
- Prime numbers after prime P with sum S
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
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.