Sum of Semi | Vector to store the primes ; Create a boolean array " prime [ 0 . . n ] " ; Initialize all prime values to be true ; If prime [ p ] is not changed then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked ; Print all prime numbers ; Function to return the semi - prime sum ; Variable to store the sum of semi - primes ; Iterate over the prime values ; Break the loop once the product exceeds N ; Add valid products which are less than or equal to N each product is a semi - prime number ; Driver code
let pr = [ ] ; let prime = new Array ( 10000000 + 1 ) ; function sieve ( n ) { for ( let i = 2 ; i <= n ; i += 1 ) { prime [ i ] = 1 ; } for ( let p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( let i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( let p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) pr . push ( p ) ; } function SemiPrimeSum ( N ) { let ans = 0 ; for ( let i = 0 ; i < pr . length ; i += 1 ) { for ( let j = i ; j < pr . length ; j += 1 ) { if ( pr [ i ] * pr [ j ] > N ) break ; ans += pr [ i ] * pr [ j ] ; } } return ans ; } let N = 6 ; sieve ( N ) ; document . write ( SemiPrimeSum ( N ) ) ;