Product of all the Composite Numbers in an array | Function that returns the the product of all composite numbers ; Find maximum value in the array ; Use sieve to find all prime numbers less than or equal to max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Set 0 and 1 as primes as they don 't need to be  counted as composite numbers ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Find the product of all composite numbers in the arr [ ] ; Driver code
< ? php function compositeProduct ( $ arr , $ n ) { $ max_val = max ( $ arr ) ; $ prime = array_fill ( 0 , $ max_val + 1 , true ) ; $ prime [ 0 ] = true ; $ prime [ 1 ] = true ; for ( $ p = 2 ; $ p * $ p <= $ max_val ; $ p ++ ) { if ( $ prime [ $ p ] == true ) { for ( $ i = $ p * 2 ; $ i <= $ max_val ; $ i += $ p ) $ prime [ $ i ] = false ; } } $ product = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) if ( ! $ prime [ $ arr [ $ i ] ] ) { $ product *= $ arr [ $ i ] ; } return $ product ; } $ arr = array ( 2 , 3 , 4 , 5 , 6 , 7 ) ; $ n = count ( $ arr ) ; echo compositeProduct ( $ arr , $ n ) ; ? >