![]() |
Given a positive integer N, the task is to find the number of unordered pairs of semi-prime numbers in the range [1, N] such that their sum is prime. Examples:
Approach: The given problem can be solved by using the Sieve of Eratosthenes. An array prime[] can be created where prime[i] stores the distinct prime factors of the number using the Sieve. All numbers in the range [1, N] with 2 distinct prime factors can be stored in a vector semiPrimes. Thereafter, iterate over all unordered pairs of the vector semiPrimes and maintain the count of pairs with prime sum. Below is the implementation of the above approach: C++
Java
Python3
C#
Javascript
Output:
313
Time Complexity: O(N2)
|
Reffered: https://www.geeksforgeeks.org
Mathematical |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |