Count of unordered pairs of semi | python program of 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
maxn = 100000 NEW_LINE prime = [ 0 for _ in range ( maxn ) ] NEW_LINE def semiPrimes ( N ) : NEW_LINE INDENT for p in range ( 2 , maxn ) : NEW_LINE INDENT if ( prime [ p ] == 0 ) : NEW_LINE INDENT for i in range ( 2 * p , maxn , p ) : NEW_LINE INDENT prime [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT sPrimes = [ ] NEW_LINE for p in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( prime [ p ] == 2 ) : NEW_LINE INDENT sPrimes . append ( p ) NEW_LINE DEDENT DEDENT return sPrimes NEW_LINE DEDENT def countPairs ( semiPrimes ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 0 , len ( semiPrimes ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( semiPrimes ) ) : NEW_LINE INDENT if ( prime [ semiPrimes [ i ] + semiPrimes [ j ] ] == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 100 NEW_LINE print ( countPairs ( semiPrimes ( N ) ) ) NEW_LINE DEDENT