Open In App

Product of all primes in the range from L to R

Improve
Improve
Like Article
Like
Save
Share
Report

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;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)


C




// C program to find product of primes
// in range L to R
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define mod 1000000007
const int MAX = 10000;
 
// Function to build the prefix product array
void buildPrefix(int prefix[])
{
    // 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, int prefix[])
{
    return (prefix[R] * inverse(prefix[L - 1])) % mod;
}
 
// Driver code
int main()
{
    int prefix[MAX + 1];
    buildPrefix(prefix);
    int L = 10, R = 20;
    printf("%lld", productPrimeRange(L, R, prefix));
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)


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 Aditya Kumar (adityakumar129)


Python 3




# Python 3 program to find product of primes
# in range L to R
 
mod = 1000000007
MAX = 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


Javascript




<script>
 
// Javascript program to find product of primes
// in range L to R
var mod = 100000007
var MAX = 10000;
 
// prefix[i] is going to store product of primes
// till i (including i).
var prefix = Array(MAX+1);
 
// Function to build the prefix product array
function 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.
    var prime = Array(MAX+1).fill(true);
 
    for (var 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 (var i = p * 2; i <= MAX; i += p)
                prime[i] = false;
        }
    }
 
    // Build prefix array
    prefix[0] = prefix[1] = 1;
    for (var 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) */
function power(x, y, p)
{
 
    // Initialize result
    var 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
function inverse( n)
{
    return power(n, mod - 2, mod);
}
 
// Function to return product of prime in range
function productPrimeRange(L, R)
{
 
    return (prefix[R] * inverse(prefix[L - 1])) % mod;
}
 
// Driver code
buildPrefix();
var L = 10, R = 20;
document.write( productPrimeRange(L, R));
 
</script>


Output: 

46189

 



Last Updated : 28 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads