Product of all primes in the range from L to R
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.
Examples:
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:
C++
// CPP program to find product of primes // in range L to R #include <bits/stdc++.h> using namespace std; #define mod 1000000007 const int MAX = 10000; // prefix[i] is going to store product of primes // till i (including i). int prefix[MAX + 1]; // Function to build the prefix product array void buildPrefix() { // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. bool prime[MAX + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 1; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) prefix[p] = (prefix[p] * p) % mod; } } /* Iterative Function to calculate (x^y)%p in O(log y) */long long int power(long long int x, long long int y, int p) { // Initialize result long long int res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now // y = y/2 y = y >> 1; x = (x * x) % p; } return res; } // Returns modular inverse long long int inverse(long long int n) { return power(n, mod - 2, mod); } // Function to return product of prime in range long long int productPrimeRange(int L, int R) { return (prefix[R] * inverse(prefix[L - 1])) % mod; } // Driver code int main() { buildPrefix(); int L = 10, R = 20; cout << productPrimeRange(L, R) << endl; return 0; } |
Java
// Java program to find product of primes // in range L to R import java.io.*; class GFG { static int mod = 1000000007; static int MAX = 10000; // prefix[i] is going to store product of primes // till i (including i). static int []prefix = new int[MAX+1]; // Function to build the prefix product array static void buildPrefix() { // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. boolean prime[] = new boolean[MAX + 1]; for(int i=0;i<MAX +1;i++) prime[i]= true; //memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 1; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) prefix[p] = (prefix[p] * p) % mod; } } /* Iterative Function to calculate (x^y)%p in O(log y) */static long power(long x, long y, int p) { // Initialize result long res = 1; // Update x if it is more than or // equal to p x = x % p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1)>0) res = (res * x) % p; // y must be even now // y = y/2 y = y >> 1; x = (x * x) % p; } return res; } // Returns modular inverse static long inverse(long n) { return power(n, mod - 2, mod); } // Function to return product of prime in range static long productPrimeRange(int L, int R) { return (prefix[R] * inverse(prefix[L - 1])) % mod; } // Driver code public static void main (String[] args) { buildPrefix(); int L = 10, R = 20; System.out.println( productPrimeRange(L, R)); } } //This code is contributed by shs.. |
Python 3
# Python 3 program to find product of primes # in range L to R mod = 1000000007MAX = 10000 # prefix[i] is going to store product of primes # till i (including i). prefix = [0]*(MAX + 1) # Function to build the prefix product array def buildPrefix(): # Create a boolean array "prime[0..n]". A # value in prime[i] will finally be false # if i is Not a prime, else true. prime = [True]*(MAX + 1) p = 2 while p * p <= MAX : # If prime[p] is not changed, then # it is a prime if (prime[p] == True) : # Update all multiples of p for i in range( p * 2, MAX+1, p): prime[i] = False p += 1 # Build prefix array prefix[0] = prefix[1] = 1 for p in range(2,MAX+1) : prefix[p] = prefix[p - 1] if (prime[p]): prefix[p] = (prefix[p] * p) % mod # Iterative Function to calculate # (x^y)%p in O(log y) def power(x, y,p): # Initialize result res = 1 # Update x if it is more than or # equal to p x = x % p while (y > 0) : # If y is odd, multiply x with result if (y & 1): res = (res * x) % p # y must be even now # y = y//2 y = y >> 1 x = (x * x) % p return res # Returns modular inverse def inverse( n): return power(n, mod - 2, mod) # Function to return product of prime in range def productPrimeRange(L, R): return (prefix[R] * inverse(prefix[L - 1])) % mod # Driver code if __name__ == "__main__": buildPrefix() L = 10 R = 20 print(productPrimeRange(L, R)) # this code is contributed by # ChitraNayal |
C#
// C# program to find product of // primes in range L to R using System; class GFG { static int mod = 1000000007; static int MAX = 10000; // prefix[i] is going to store product // of primes till i (including i). static int []prefix = new int[MAX + 1]; // Function to build the prefix // product array static void buildPrefix() { // Create a boolean array "prime[0..n]". // A value in prime[i] will finally be // false if i is Not a prime, else true. bool []prime = new bool[MAX + 1]; for(int i = 0; i < MAX + 1; i++) prime[i] = true; //memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 1; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) prefix[p] = (prefix[p] * p) % mod; } } /* Iterative Function to calculate (x^y)%p in O(log y) */static long power(long x, long y, int p) { // Initialize result long res = 1; // Update x if it is more // than or equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if ((y & 1) > 0) res = (res * x) % p; // y must be even now // y = y/2 y = y >> 1; x = (x * x) % p; } return res; } // Returns modular inverse static long inverse(long n) { return power(n, mod - 2, mod); } // Function to return product // of prime in range static long productPrimeRange(int L, int R) { return (prefix[R] * inverse(prefix[L - 1])) % mod; } // Driver code public static void Main () { buildPrefix(); int L = 10, R = 20; Console.WriteLine(productPrimeRange(L, R)); } } // This code is contributed by anuj_67 |
46189
Recommended Posts:
- Product of Primes of all Subsets
- Sum of all Primes in a given range using Sieve of Eratosthenes
- K-Primes (Numbers with k prime factors) in a 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
- Find the number in a range having maximum product of the digits
- Queries to find maximum product pair in range with updates
- Circular primes less than n
- Palindromic Primes
- Count Primes in Ranges
- Find the sum of all Truncatable primes below N
- Maximum Primes whose sum is equal to given N
- Print all safe primes below N
- Print all multiplicative primes <= N
- Print all Proth primes up to N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



