Find the Product of first N Prime Numbers
Last Updated :
19 Jul, 2022
Given a positive integer N, calculate the product of the first N prime numbers.
Examples:
Input : N = 3
Output : 30
Explanation : First 3 prime numbers are 2, 3, 5.
Input : N = 5
Output : 2310
Approach:
- Create a sieve which will help us to identify if the number is prime or not in O(1) time.
- Run a loop starting from 1 until and unless we find n prime numbers.
- Multiply all the prime numbers and neglect those which are not prime.
- Then, display the product of 1st N prime numbers.
Time Complexity – O( Nlog(logN) )
Below is the implementation of above approach:
C++
#include "cstring"
#include <iostream>
using namespace std;
#define MAX 10000
bool prime[MAX + 1];
void SieveOfEratosthenes()
{
memset (prime, true , sizeof (prime));
prime[1] = false ;
for ( int p = 2; p * p <= MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
int solve( int n)
{
int count = 0, num = 1;
long long int prod = 1;
while (count < n) {
if (prime[num]) {
prod *= num;
count++;
}
num++;
}
return prod;
}
int main()
{
SieveOfEratosthenes();
int n = 5;
cout << solve(n);
return 0;
}
|
Java
class GFG
{
static int MAX= 10000 ;
static boolean [] prime= new boolean [MAX + 1 ];
static void SieveOfEratosthenes()
{
prime[ 1 ] = true ;
for ( int p = 2 ; p * p <= MAX; p++) {
if (prime[p] == false ) {
for ( int i = p * 2 ; i <= MAX; i += p)
prime[i] = true ;
}
}
}
static int solve( int n)
{
int count = 0 , num = 1 ;
int prod = 1 ;
while (count < n) {
if (!prime[num]) {
prod *= num;
count++;
}
num++;
}
return prod;
}
public static void main(String[] args)
{
SieveOfEratosthenes();
int n = 5 ;
System.out.println(solve(n));
}
}
|
C#
class GFG
{
static int MAX=10000;
static bool [] prime= new bool [MAX + 1];
static void SieveOfEratosthenes()
{
prime[1] = true ;
for ( int p = 2; p * p <= MAX; p++) {
if (prime[p] == false ) {
for ( int i = p * 2; i <= MAX; i += p)
prime[i] = true ;
}
}
}
static int solve( int n)
{
int count = 0, num = 1;
int prod = 1;
while (count < n) {
if (!prime[num]) {
prod *= num;
count++;
}
num++;
}
return prod;
}
public static void Main()
{
SieveOfEratosthenes();
int n = 5;
System.Console.WriteLine(solve(n));
}
}
|
Python
import math as mt
MAX = 10000
prime = [ True for i in range ( MAX + 1 )]
def SieveOfErastosthenes():
prime[ 1 ] = False
for p in range ( 2 ,mt.ceil(mt.sqrt( MAX ))):
if prime[p]:
for i in range ( 2 * p, MAX + 1 ,p):
prime[i] = False
def solve(n):
count,num = 0 , 1
prod = 1
while count<n:
if prime[num]:
prod * = num
count + = 1
num + = 1
return prod
SieveOfErastosthenes()
n = 5
print (solve(n))
|
PHP
<?php
$MAX = 10000;
$prime = array_fill (0, $MAX + 1, true);
function SieveOfEratosthenes()
{
global $MAX ;
global $prime ;
$prime = array_fill (0, $MAX + 1, true);
$prime [1] = false;
for ( $p = 2; $p * $p <= $MAX ; $p ++)
{
if ( $prime [ $p ] == true)
{
for ( $i = $p * 2; $i <= $MAX ; $i += $p )
$prime [ $i ] = false;
}
}
}
function solve( $n )
{
global $prime ;
$count = 0;
$num = 1;
$prod = 1;
while ( $count < $n )
{
if ( $prime [ $num ]== true)
{
$prod *= $num ;
$count ++;
}
$num ++;
}
return $prod ;
}
SieveOfEratosthenes();
$n = 5;
echo solve( $n );
?>
|
Javascript
<script>
let MAX = 10000;
let prime = new Array(MAX + 1).fill( true );
function SieveOfEratosthenes()
{
prime[1] = false ;
for (let p = 2; p * p <= MAX; p++)
{
if (prime[p] == true )
{
for (let i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
function solve(n)
{
let count = 0;
let num = 1;
let prod = 1;
while (count < n)
{
if (prime[num]== true )
{
prod *= num;
count++;
}
num++;
}
return prod;
}
SieveOfEratosthenes();
let n = 5;
document.write(solve(n));
</script>
|
Auxiliary Space: O(MAX)
NOTE: For larger values of N, the product may be give integer overflow errors.
Also for multiple queries, prefix array technique can be used which will give output of each query in O(1) after making the prefix array first which will take O(N) time.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...