Given a range [L, R]. The task is to find the product of all the prime numbers in the given range from L to R both inclusive modulo 10^9 + 7.
Input: L = 10, R = 20 Output: 46189 Prime numbers between [10, 20] are: 11, 13, 17, 19 Therefore, product = 11 * 13 * 17 * 19 = 46189 Input: L = 15, R = 25 Output: 7429
A Simple Solution is to traverse from L to R, check if the current number is prime. If yes, multiply it with product. Finally, print the product.
An Efficient Solution is to use Sieve of Eratosthenes to find all primes up to a given limit. Then, compute a prefix product array to store product till every value before the limit. Once we have prefix array, We just need to return (prefix[R] *modular_inverse( prefix[L-1]))%(10^9+7).
Note: prefix[i] will store the product of all prime numbers from 1 to i.
Below is the implementation of above approach:
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Product of Primes of all Subsets
- Minimum difference between any two primes from the given range
- Sum of all Primes in a given range using Sieve of Eratosthenes
- Find Square Root under Modulo p | (When p is product of two primes in the form 4*i + 3)
- K-Primes (Numbers with k prime factors) in a range
- Count of primes in a given range that can be expressed as sum of perfect squares
- Length of largest sub-array having primes strictly greater than non-primes
- Minimum window size containing atleast P primes in every window of given range
- Count primes that can be expressed as sum of two consecutive primes and 1
- Count of primes below N which can be expressed as the sum of two primes
- Count of divisors of product of an Array in range L to R for Q queries
- Check if product of Array elements in given range are M-th root or not
- Find the number in a range having maximum product of the digits
- Sum of product of proper divisors of all Numbers lying in range [L, R]
- Queries for Count of divisors of product of an Array in given range | Set 2 (MO's Algorithm)
- Queries to find maximum product pair in range with updates
- Count of index pairs in array whose range product is a positive integer
- Circular primes less than n
- Palindromic Primes
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.