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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Sum and product of k smallest and k largest prime numbers in the array
- Check if each element of the given array is the product of exactly K prime numbers
- Check if product of array containing prime numbers is a perfect square
- Find the row whose product has maximum count of prime factors
- Find two numbers with sum and product both same as N
- Find three prime numbers with given sum
- Find two prime numbers with given sum
- Find the XOR of first N Prime Numbers
- Find if n can be written as product of k numbers
- Program to find the LCM of two prime numbers
- Program to find sum of prime numbers between 1 to n
- Find count of Almost Prime numbers from 1 to N
- Find the sum of prime numbers in the Kth array
- Find out the prime numbers in the form of A+nB or B+nA
- Program to find Prime Numbers Between given Interval