给定整数N ,任务是计算范围[1,N]中的质数对的数量,以使每对元素之间的差也是质数。
例子:
Input: N = 5
Output: 2
Explanations:
Pair of prime numbers in the range [1, 5] whose difference between elements is also a prime number are:
(2, 5) = 3 (Prime number)
(3, 5) = 2 (Prime number)
Therefore, the count of pairs of the prime numbers whose difference is also a prime number is 2.
Input: N = 11
Output: 4
天真的方法:解决此问题的最简单方法是生成范围为[1,N]的所有可能的元素对,并针对每个对检查两个元素以及该对中两个元素之间的差是否是质数是否编号。如果发现为真,则增加计数。最后,打印计数。
时间复杂度: O(N 2 *√N)
辅助空间: O(1)
高效方法:为了优化上述方法,该思想基于以下观察结果:
Odd number – Even Number = Odd Number
Odd number – Odd number = Even number
2 is the only even prime number.
Therefore, the problem reduces to check only for those pairs of prime numbers whose difference between the elements of the pair is equal to 2.
请按照以下步骤解决问题:
- 初始化一个变量,例如cntPairs,以存储质数对的计数,以使每对元素之间的差也是质数。
- 初始化一个数组,说sieve []以检查[1,N]范围内的数字是否是质数。
- 使用Eratosthenes筛子找出[1,N]范围内的所有质数。
- 遍历范围[2,N] ,对于给定范围内的每个元素,检查sieve [i]和sieve [i – 2]是否为true。如果确定为true,则将cntPairs的值增加2 。
- 最后,打印cntPairs的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find all prime
// numbers in the range [1, N]
vector SieveOfEratosthenes(
int N)
{
// isPrime[i]: Stores if i is
// a prime number or not
vector isPrime(N, true);
isPrime[0] = false;
isPrime[1] = false;
// Calculate all prime numbers up to
// Max using Sieve of Eratosthenes
for (int p = 2; p * p <= N; p++) {
// If P is a prime number
if (isPrime[p]) {
// Set all multiple of P
// as non-prime
for (int i = p * p; i <= N;
i += p) {
// Update isPrime
isPrime[i] = false;
}
}
}
return isPrime;
}
// Function to count pairs of
// prime numbers in the range [1, N]
// whose difference is prime
int cntPairsdiffOfPrimeisPrime(int N)
{
// Function to count pairs of
// prime numbers whose difference
// is also a prime number
int cntPairs = 0;
// isPrime[i]: Stores if i is
// a prime number or not
vector isPrime
= SieveOfEratosthenes(N);
// Iterate over the range [2, N]
for (int i = 2; i <= N; i++) {
// If i and i - 2 is
// a prime number
if (isPrime[i] &&
isPrime[i - 2]) {
// Update cntPairs
cntPairs += 2;
}
}
return cntPairs;
}
// Driver Code
int main()
{
int N = 5;
cout << cntPairsdiffOfPrimeisPrime(N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find all prime
// numbers in the range [1, N]
public static boolean[] SieveOfEratosthenes(int N)
{
// isPrime[i]: Stores if i is
// a prime number or not
boolean[] isPrime = new boolean[N + 1];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
// Calculate all prime numbers up to
// Max using Sieve of Eratosthenes
for(int p = 2; p * p <= N; p++)
{
// If P is a prime number
if (isPrime[p])
{
// Set all multiple of P
// as non-prime
for(int i = p * p; i <= N; i += p)
{
// Update isPrime
isPrime[i] = false;
}
}
}
return isPrime;
}
// Function to count pairs of
// prime numbers in the range [1, N]
// whose difference is prime
public static int cntPairsdiffOfPrimeisPrime(int N)
{
// Function to count pairs of
// prime numbers whose difference
// is also a prime number
int cntPairs = 0;
// isPrime[i]: Stores if i is
// a prime number or not
boolean[] isPrime = SieveOfEratosthenes(N);
// Iterate over the range [2, N]
for(int i = 2; i <= N; i++)
{
// If i and i - 2 is
// a prime number
if (isPrime[i] && isPrime[i - 2])
{
// Update cntPairs
cntPairs += 2;
}
}
return cntPairs;
}
// Driver Code
public static void main(String args[])
{
int N = 5;
System.out.println(cntPairsdiffOfPrimeisPrime(N));
}
}
// This code is contributed by hemanth gadarla
Python3
# Python3 program to implement
# the above approach
from math import sqrt
# Function to find all prime
# numbers in the range [1, N]
def SieveOfEratosthenes(N):
# isPrime[i]: Stores if i is
# a prime number or not
isPrime = [True for i in range(N + 1)]
isPrime[0] = False
isPrime[1] = False
# Calculate all prime numbers up to
# Max using Sieve of Eratosthenes
for p in range(2, int(sqrt(N)) + 1, 1):
# If P is a prime number
if (isPrime[p]):
# Set all multiple of P
# as non-prime
for i in range(p * p, N + 1, p):
# Update isPrime
isPrime[i] = False
return isPrime
# Function to count pairs of
# prime numbers in the range [1, N]
# whose difference is prime
def cntPairsdiffOfPrimeisPrime(N):
# Function to count pairs of
# prime numbers whose difference
# is also a prime number
cntPairs = 0
# isPrime[i]: Stores if i is
# a prime number or not
isPrime = SieveOfEratosthenes(N)
# Iterate over the range [2, N]
for i in range(2, N + 1, 1):
# If i and i - 2 is
# a prime number
if (isPrime[i] and isPrime[i - 2]):
# Update cntPairs
cntPairs += 2
return cntPairs
# Driver Code
if __name__ == '__main__':
N = 5
print(cntPairsdiffOfPrimeisPrime(N))
# This code is contributed by ipg2016107
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find all prime
// numbers in the range [1, N]
public static bool[] SieveOfEratosthenes(int N)
{
// isPrime[i]: Stores if i is
// a prime number or not
bool[] isPrime = new bool[N + 1];
for(int i = 0; i < N + 1; i++)
{
isPrime[i] = true;
}
isPrime[0] = false;
isPrime[1] = false;
// Calculate all prime numbers up to
// Max using Sieve of Eratosthenes
for(int p = 2; p * p <= N; p++)
{
// If P is a prime number
if (isPrime[p])
{
// Set all multiple of P
// as non-prime
for(int i = p * p; i <= N; i += p)
{
// Update isPrime
isPrime[i] = false;
}
}
}
return isPrime;
}
// Function to count pairs of
// prime numbers in the range [1, N]
// whose difference is prime
public static int cntPairsdiffOfPrimeisPrime(int N)
{
// Function to count pairs of
// prime numbers whose difference
// is also a prime number
int cntPairs = 0;
// isPrime[i]: Stores if i is
// a prime number or not
bool[] isPrime = SieveOfEratosthenes(N);
// Iterate over the range [2, N]
for(int i = 2; i <= N; i++)
{
// If i and i - 2 is
// a prime number
if (isPrime[i] && isPrime[i - 2])
{
// Update cntPairs
cntPairs += 2;
}
}
return cntPairs;
}
// Driver Code
public static void Main()
{
int N = 5;
Console.WriteLine(cntPairsdiffOfPrimeisPrime(N));
}
}
// This code is contributed by susmitakundugoaldanga
2
时间复杂度: O(N * log(log(N)))
辅助空间: O(N)