Product of Primes | Practice | GeeksforGeeks

Given two numbers L and R (inclusive) find the product of primes within this range. Print the product modulo 109+7. If there are no primes in that range you must print 1.
Example 1:
Input: L = 1, R = 10
Output: 210
Explaination: The prime numbers are 
2, 3, 5 and 7.
Example 2:
Input: L = 1, R = 20
Output: 9699690
Explaination: The primes are 2, 3, 
5, 7, 11, 13, 17 and 19.
Your Task:
You do not need to read input or print anything. Your task is to complete the function primeProduct() which takes L and R and returns the product of the primes within the range. If there are no primes in that range then return 1.
Expected Time Complexity: O((R-L)*(logR))
Expected Auxiliary Space: O(sqrt(R))
Constraints:
1 ≤ L ≤ R ≤ 109
0 ≤ L - R ≤ 106  
+1
jaswanth17121 hour ago
Best c++ efficient way
#define ll long longclass Solution{public:bool check(ll n){      if(n==1)   return false;      if(n==2)   return true;   if(n%2==0)   return false;      for(int i=3;i*i<=n;i+=2){       if(n%i==0)       return false;   }   return true;   }   long long primeProduct(long long L, long long R){       // code here       ll ans=1;       ll mod=1e9+7;       for(ll i=L;i<=R;i++){           if(check(i))           ans=(ll)((ans*i)%(mod));       }              return ans%mod;   }
0
mayank20452 hours ago
[Java] Memory efficient solution to find product of primes within a range given.
class Solution{
    static long primeProduct(long L, long R){
        // code here
        long mod = 1000000007;
        int SIZE = (int) (R - L + 1);
        // By default value is false, which means that value is prime
        boolean primes[] = new boolean[SIZE];
        for (long i = 2; i <= R / 2; ++i) {
            long start = L % i == 0 ? L : L + (i - (L % i));
            //We are checking start is equal to i or not because when L is smaller than 
            //i, then start will be equal to i as per code above but in this case start or i
            //is a prime, so we cannot mark primes[start] true (if it is false), so we will
            //increment start to its next value
            if (start == i) {
                start = start + i;
            }
            int index = 0;
            while (start <= R) {
                index = (int) (start - L);
                primes[index] = true;

                start += i;
            }
        }

        long product = 1;

        for (int i = 0; i < SIZE; ++i) {
            if (primes[i] == false) {
                product = (product * (L + i)) % mod;
            }
        }

        return product;
    }
}
0
irejwanul3 hours ago
The output is not succeeding for input: 34524465 34551428
+1
surjit200120013 hours ago
C++ Solution Using segmented sieve
class Solution{
    const int MOD=1000000007;
    bool prime[1000001];
    vector<int> p;
    void sieve(){
        prime[0]=prime[1]=1;
        for(int i=2;1LL*i*i<1000001;i++){
            if(!prime[i]){
                for(int j=i*i;j<1000001;j+=i){
                    prime[j]=1;
                }
            }
        }
        for(int i=2;i<1000001;i++)if(!prime[i])p.push_back(i);
    }
    int seg(int L,int R){
        if(L==1)L++;
        int max=(R-L+1),res=1;
        vector<bool> arr(max,false);
        for(auto &x:p){
            if(1LL*x*x<=R){
                long long i=(L/x)*x;
                if(i<L)i+=x;
                for(;i<=R;i+=x){
                    if(i!=x)arr[i-L]=1;
                }
            }
        }
        for(int i=L;i<=R;i++)if(!arr[i-L])res=1LL*res*i%MOD;
        return res;
    }
public:
    long long primeProduct(long long L, long long R){
        sieve();
        return seg(L,R);
    }
};
0
aayush nigam3 hours ago
My Solution:
class Solution{   static long primeProduct(long L, long R){       // code here       OptionalLong result = LongStream.rangeClosed(a, b).filter(i->isPrime(i)).reduce((x,y)->x*y);        return result.getAsLong()%10000007;   }       private static boolean isPrime(long n){        if(n==1)           return false;
        boolean result = LongStream.rangeClosed(2, n/2).noneMatch(i->n%i==0);       return result;    }}
+3
ameyakannurkar3 hours ago
class Solution{    bool isprime(long long n){       for(long long i=2;i*i<=n;i++)           if(n%i == 0)               return false;       return true;   }public:   long long primeProduct(long long L, long long R){       // code here         long long product=1;       for(long long i=L;i<=R;i++)           if(isprime(i))               product = (product*i) % 1000000007;       return product;   }};
0
subhashishde083 hours ago
bool isPrime(long long int n)
{
 
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    if (n % 2 == 0 || n % 3 == 0)
        return false;
  
    for (long long int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
  
    return true;
}
    long long primeProduct(long long L, long long R){
        // code here
        long long int mod = pow(10,9) + 7;
        long long int result = 1;
        for(long long int i=L;i<=R;i++){
            if(isPrime(i)){
                result *= i;
                result = result % mod;
            }
        }
        return result%mod;
    }
};
0
rohankundu8594 hours ago
//C++ Solution 
 
bool isprime(long long int n){   bool p=true;   for(int i=2;i<=sqrt(n);i++)   {       if(n%i==0)       {           p=false;           break;       }   }   return p;}   long long primeProduct(long long L, long long R)   {       // code here       long long int x=1;       for( long long int j=L;j<=R;j++)       {           if(isprime(j))           {               x=(x*j)%1000000007;           }       }      return x;   }
0
choprakartik104 hours ago
Code in JAVA(1.8)

class Solution{
   static long primeProduct(long L, long R){
       long output = 1;
       for(long i=L;i<=R;i++){
           if(isPrime(i)){
              // System.out.println(i);
        output = (output*i)%1000000007;
           }
       }
       
       return output;
   }
   static boolean isPrime(long n)
   {
       if (n <= 1)
           return false;
       else if (n == 2)
           return true;
       else if (n % 2 == 0)
           return false;

       for (long i = 3; i <= Math.sqrt(n); i =i+2)
       {
           if (n % i == 0)
               return false;
       }
       return true;
   }
}
0
kunalbandooni14 hours ago
Simple C++ solution:-
Fastest in its kind :)
class Solution{
    bool isprime(long long n){
        for(long long i=2;i*i<=n;i++)
            if(n%i == 0)
                return false;
        return true;
    }
public:
    long long primeProduct(long long L, long long R){
        long long product=1;
        for(long long i=L;i<=R;i++)
            if(isprime(i))
                product = (product*i) % 1000000007;
        return product;
    }
};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.