素数和在 [1, N] 范围内的无序半素数对的计数
给定一个正整数N ,任务是找到[1, N]范围内的无序半素数对的数量,使得它们的和为素数。
例子:
Input: N = 25
Output: 5
Explanation:
The valid pairs of semi-prime numbers whose sum is also prime are (10, 21), (14, 15), (15, 22), (20, 21), and (21, 22). The count of such numbers is 5.
Input: N = 100
Output: 313
方法:给定的问题可以通过使用埃拉托色尼筛法来解决。可以创建数组prime[] ,其中prime[i]使用 Sieve 存储数字的不同素因子。 [1, N] 范围内具有 2 个不同素数的所有数字都可以存储在向量semiPrimes中。此后,遍历向量semiPrimes的所有无序对并保持具有素数和的对的计数。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
using namespace std;
const int maxn = 100000;
// Stores the count of distinct prime
// number in factor of current number
int prime[maxn] = {};
// Function to return the vector of
// semi prime numbers in range [1, N]
vector semiPrimes(int N)
{
// Count of distinct prime number
// in the factor of current number
// using Sieve of Eratosthenes
for (int p = 2; p <= maxn; p++) {
// If current number is prime
if (prime[p] == 0) {
for (int i = 2 * p; i <= maxn; i += p)
prime[i]++;
}
}
// Stores the semi prime numbers
vector sPrimes;
for (int p = 2; p <= N; p++)
// If p has 2 distinct prime factors
if (prime[p] == 2)
sPrimes.push_back(p);
// Return vector
return sPrimes;
}
// Function to count unordered pairs of
// semi prime numbers with prime sum
int countPairs(vector semiPrimes)
{
// Stores the final count
int cnt = 0;
// Loop to iterate over al the
// l unordered pairs
for (int i = 0;
i < semiPrimes.size(); i++) {
for (int j = i + 1;
j < semiPrimes.size(); j++) {
// If sum of current semi prime
// numbers is a prime number
if (prime[semiPrimes[i]
+ semiPrimes[j]]
== 0) {
cnt++;
}
}
}
// Return answer
return cnt;
}
// Driver Code
int main()
{
int N = 100;
cout << countPairs(semiPrimes(N));
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
static int maxn = 100000;
// Stores the count of distinct prime
// number in factor of current number
static int[] prime = new int[maxn + 1];
// Function to return the vector of
// semi prime numbers in range [1, N]
static ArrayList semiPrimes(int N)
{
// Count of distinct prime number
// in the factor of current number
// using Sieve of Eratosthenes
for (int p = 2; p <= maxn; p++) {
// If current number is prime
if (prime[p] == 0) {
for (int i = 2 * p; i <= maxn; i += p)
prime[i]++;
}
}
// Stores the semi prime numbers
ArrayList sPrimes
= new ArrayList();
for (int p = 2; p <= N; p++)
// If p has 2 distinct prime factors
if (prime[p] == 2)
sPrimes.add(p);
// Return vector
return sPrimes;
}
// Function to count unordered pairs of
// semi prime numbers with prime sum
static int countPairs(ArrayList semiPrimes)
{
// Stores the final count
int cnt = 0;
// Loop to iterate over al the
// l unordered pairs
for (int i = 0; i < semiPrimes.size(); i++) {
for (int j = i + 1; j < semiPrimes.size(); j++) {
// If sum of current semi prime
// numbers is a prime number
if (prime[semiPrimes.get(i) + semiPrimes.get(j)] == 0) {
cnt++;
}
}
}
// Return answer
return cnt;
}
// Driver Code
public static void main(String[] args)
{
int N = 100;
System.out.print(countPairs(semiPrimes(N)));
}
}
// This code is contributed by code_hunt.
Python3
# python program of the above approach
maxn = 100000
# Stores the count of distinct prime
# number in factor of current number
prime = [0 for _ in range(maxn)]
# Function to return the vector of
# semi prime numbers in range [1, N]
def semiPrimes(N):
# Count of distinct prime number
# in the factor of current number
# using Sieve of Eratosthenes
for p in range(2, maxn):
# If current number is prime
if (prime[p] == 0):
for i in range(2*p, maxn, p):
prime[i] += 1
# Stores the semi prime numbers
sPrimes = []
for p in range(2, N+1):
# If p has 2 distinct prime factors
if (prime[p] == 2):
sPrimes.append(p)
# Return vector
return sPrimes
# Function to count unordered pairs of
# semi prime numbers with prime sum
def countPairs(semiPrimes):
# Stores the final count
cnt = 0
# Loop to iterate over al the
# l unordered pairs
for i in range(0, len(semiPrimes)):
for j in range(i+1, len(semiPrimes)):
# If sum of current semi prime
# numbers is a prime number
if (prime[semiPrimes[i] + semiPrimes[j]] == 0):
cnt += 1
# Return answer
return cnt
# Driver Code
if __name__ == "__main__":
N = 100
print(countPairs(semiPrimes(N)))
# This code is contributed by rakeshsahni
C#
// C# program of the above approach
using System;
using System.Collections.Generic;
class GFG {
const int maxn = 100000;
// Stores the count of distinct prime
// number in factor of current number
static int[] prime = new int[maxn + 1];
// Function to return the vector of
// semi prime numbers in range [1, N]
static List semiPrimes(int N)
{
// Count of distinct prime number
// in the factor of current number
// using Sieve of Eratosthenes
for (int p = 2; p <= maxn; p++) {
// If current number is prime
if (prime[p] == 0) {
for (int i = 2 * p; i <= maxn; i += p)
prime[i]++;
}
}
// Stores the semi prime numbers
List sPrimes = new List();
for (int p = 2; p <= N; p++)
// If p has 2 distinct prime factors
if (prime[p] == 2)
sPrimes.Add(p);
// Return vector
return sPrimes;
}
// Function to count unordered pairs of
// semi prime numbers with prime sum
static int countPairs(List semiPrimes)
{
// Stores the final count
int cnt = 0;
// Loop to iterate over al the
// l unordered pairs
for (int i = 0; i < semiPrimes.Count; i++) {
for (int j = i + 1; j < semiPrimes.Count; j++) {
// If sum of current semi prime
// numbers is a prime number
if (prime[semiPrimes[i] + semiPrimes[j]]
== 0) {
cnt++;
}
}
}
// Return answer
return cnt;
}
// Driver Code
public static void Main()
{
int N = 100;
Console.WriteLine(countPairs(semiPrimes(N)));
}
}
// This code is contributed by ukasp.
Javascript
输出:
313
时间复杂度: O(N 2 )
辅助空间: O(N)