Count of unordered pairs of semi | JavaScript Program to implement the above approach ; Stores the count of distinct prime number in factor of current number ; Function to return the vector of semi prime numbers in range [ 1 , N ] ; Count of distinct prime number in the factor of current number using Sieve of Eratosthenes ; If current number is prime ; Stores the semi prime numbers ; If p has 2 distinct prime factors ; Return vector ; Function to count unordered pairs of semi prime numbers with prime sum ; Stores the final count ; Loop to iterate over al the l unordered pairs ; If sum of current semi prime numbers is a prime number ; Return answer ; Driver Code
const maxn = 100000 ; let prime = new Array ( maxn ) . fill ( 0 ) ; function semiPrimes ( N ) { for ( let p = 2 ; p <= maxn ; p ++ ) { if ( prime [ p ] == 0 ) { for ( let i = 2 * p ; i <= maxn ; i += p ) prime [ i ] ++ ; } } let sPrimes = [ ] ; for ( let p = 2 ; p <= N ; p ++ ) if ( prime [ p ] == 2 ) sPrimes . push ( p ) ; return sPrimes ; } function countPairs ( semiPrimes ) { let cnt = 0 ; for ( let i = 0 ; i < semiPrimes . length ; i ++ ) { for ( let j = i + 1 ; j < semiPrimes . length ; j ++ ) { if ( prime [ semiPrimes [ i ] + semiPrimes [ j ] ] == 0 ) { cnt ++ ; } } } return cnt ; } let N = 100 ; document . write ( countPairs ( semiPrimes ( N ) ) ) ;