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
- Length of largest sub-array having primes strictly greater than non-primes
- 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
- Product of Primes of all Subsets
- 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
- Minimum difference between any two primes from the given range
- Minimum window size containing atleast P primes in every window of given range
- Count of primes in a given range that can be expressed as sum of perfect squares
- Sieve of Sundaram to print all primes smaller than n
- Print all Proth primes up to N
- Print all safe primes below N
- Print all multiplicative primes <= N
- Find the sum of all Truncatable primes below N
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Find all possible ways to Split the given string into Primes
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Count of all values of N in [L, R] such that count of primes upto N is also prime
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.