Find product of prime numbers between 1 to n

Given a number n, we need to find the product of all prime numbers between 1 to n.

Examples:

Input: 5
Output: 30
Explanation: product of prime numbers between 1 to 5 is 2 * 3 * 5 = 30 

Input : 7
Output : 210

Using Sieve of Eratosthenes to find all prime numbers from 1 to n then compute the product.

Following is the algorithm to find all the prime numbers less than or equal to a given integer n by Eratosthenes’ method:

  1. Create a list of consecutive integers from 2 to n: (2, 3, 4, …,  n).
  2. Initially, let p equal 2, the first prime number.
  3. Starting from p, count up in increments of p and mark each of these numbers greater than p itself in the list. These numbers will be 2p, 3p, 4p, etc.; note that some of them may have already been marked.
  4. Find the first number greater than p in the list that is not marked. If there was no such number, stop. Otherwise, let p now equal this number (which is the next prime), and repeat from step 3.

When the algorithm terminates, all the numbers in the list that are not marked are prime and using a loop we compute the product of prime numbers.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find product
// of prime numbers between 1 to n
#include <bits/stdc++.h>
using namespace std;
  
// Returns product of primes in range from
// 1 to n.
long ProdOfPrimes(int n)
{
    // Array to store prime numbers
    bool prime[n + 1];
  
    // Create a boolean array "prime[0..n]"
    // and initialize all entries it as true.
    // A value in prime[i] will finally be
    // false if i is Not a prime, else true.
    memset(prime, true, n + 1);
  
    for (int p = 2; p * p <= n; 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 <= n; i += p)
                prime[i] = false;
        }
    }
  
    // Return product of primes generated
    // through Sieve.
    long prod = 1;
    for (int i = 2; i <= n; i++)
        if (prime[i])
            prod *= i;
    return prod;
}
  
// Driver code
int main()
{
    int n = 10;
    cout << ProdOfPrimes(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find product
// of prime numbers between 1 to n
import java.util.Arrays;
  
class GFG {
      
    // Returns product of primes in range from
    // 1 to n.
    static long ProdOfPrimes(int n)
    {
                
        // Array to store prime numbers
        boolean prime[]=new boolean[n + 1];
      
        // Create a boolean array "prime[0..n]"
        // and initialize all entries it as true.
        // A value in prime[i] will finally be
        // false if i is Not a prime, else true.
        Arrays.fill(prime, true);
      
        for (int p = 2; p * p <= n; 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 <= n; i += p)
                    prime[i] = false;
            }
        }
      
        // Return product of primes generated
        // through Sieve.
        long prod = 1;
  
        for (int i = 2; i <= n; i++)
            if (prime[i])
                prod *= i;
  
        return prod;
    }
      
    // Driver code
    public static void main (String[] args)
    {
          
        int n = 10;
          
        System.out.print(ProdOfPrimes(n));
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to find product
# of prime numbers between 1 to n
  
# Returns product of primes 
# in range from 1 to n.
def ProdOfPrimes(n):
  
    # Array to store prime numbers
    prime = [True for i in range(n + 1)]
  
    # Create a boolean array "prime[0..n]"
    # and initialize all entries it as true.
    # A value in prime[i] will finally be
    # false if i is Not a prime, else true.
    p = 2
    while(p * p <= n): 
  
        # If prime[p] is not changed, 
        # then it is a prime
        if (prime[p] == True): 
  
            # Update all multiples of p
            i = p * 2
            while(i <= n):
                prime[i] = False
                i += p
        p += 1
  
    # Return product of primes 
    # generated through Sieve.
    prod = 1
    for i in range(2, n+1):
        if (prime[i]):
            prod *= i
    return prod
  
# Driver code
n = 10
print(ProdOfPrimes(n))
  
# This code is contributed by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find product of
// prime numbers between 1 to n
using System;
  
public class GFG 
{
      
    // Returns product of primes 
    // in range from 1 to n.
    static long ProdOfPrimes(int n)
    {
                  
        // Array to store prime numbers
        bool []prime=new bool[n + 1];
      
        // Create a boolean array "prime[0..n]"
        // and initialize all entries it as true.
        // A value in prime[i] will finally be
        // false if i is Not a prime, else true.
        for(int i = 0; i < n + 1; i++)
            prime[i] = true;
          
        for (int p = 2; p * p <= n; 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 <= n; i += p)
                    prime[i] = false;
            }
        }
      
        // Return product of primes generated
        // through Sieve.
        long prod = 1;
  
        for (int i = 2; i <= n; i++)
            if (prime[i])
                prod *= i;
  
        return prod;
    }
      
    // Driver code
    public static void Main ()
    {
          
        int n = 10;
          
        Console.Write(ProdOfPrimes(n));
    }
}
  
// This code is contributed by Sam007

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find product
// of prime numbers between 1 to n
  
// Returns product of primes 
// in range from 1 to n.
function ProdOfPrimes($n)
{
    // Array to store prime 
    // numbers Create a boolean
    // array "prime[0..n]" and 
    // initialize all entries it 
    // as true. A value in prime[i] 
    // will finally be false if i 
    // is Not a prime, else true.
    $prime = array();
    for($i = 0; $i < $n + 1; $i++)
        $prime[$i] = true;
  
    for ($p = 2; $p * $p <= $n; $p++)
    {
  
        // If prime[p] is not changed,
        // then it is a prime
        if ($prime[$p] == true) 
        {
  
            // Update all multiples of p
            for ($i = $p * 2; $i <= $n;
                              $i += $p)
                $prime[$i] = false;
        }
    }
  
    // Return product of primes 
    // generated through Sieve.
    $prod = 1;
    for ($i = 2; $i <= $n; $i++)
        if ($prime[$i])
            $prod *= $i;
    return $prod;
}
  
// Driver Code
$n = 10;
echo (ProdOfPrimes($n));
  
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>

chevron_right


Output:

210


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Sam007, manishshaw1



Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.