Find the Product of first N Prime Numbers | Javascript implementation of above solution ; 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 . ; If prime [ p ] is not changed , then it is a prime ; Set all multiples of p to non - prime ; find the product of 1 st N prime numbers ; count of prime numbers ; product of prime numbers ; if the number is prime add it ; increase the count ; get to next number ; create the sieve ; find the value of 1 st n prime numbers
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 ) ) ;