Find the Product of first N Prime Numbers | PHP 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
< ? 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 ) ; ? >