给定整数N ,任务是从范围为[1,N]的前三个三元之和中查找不同的素数三元组的数量。
例子:
Input: N = 7
Output: 2
Explanation: All valid triplets are (2, 3, 5) and (2, 5, 7). Therefore, the required output is 2.
Input: N = 4
Output: 0
方法:想法是使用Eratosthenes筛。请按照以下步骤解决问题:
- 初始化一个数组,例如prime [] ,以使用Eratosthenes的Sieve将所有素数标记为N。
- 初始化一个数组,说cntTriplet [],存储三胞胎素数高达N的满足上述条件的计数。
- 在索引[3,N]上遍历数组cntTriplet []并检查以下条件:
- 如果prime [i]和prime [i – 2]是质数,则将cntTriplet [i]更新1 。
- 否则,分配cntTriplet [i] = cntTriplet [i – 1] 。
- 最后,打印cntTriplet [N]的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define MAX 1000001
// Boolean array to
// mark prime numbers
bool prime[MAX];
// To count the prime triplets
// having the sum of the first two
// numbers equal to the third element
int cntTriplet[MAX];
// Function to count prime triplets
// having sum of the first two elements
// equal to the third element
void primeTriplet(long N)
{
// Sieve of Eratosthenes
memset(prime, true, sizeof(prime));
prime[0] = prime[1] = false;
for (int i = 2; i * i <= N; i++) {
if (prime[i]) {
for (int j = i * i; j <= N; j += i) {
prime[j] = false;
}
}
}
for (int i = 3; i <= N; i++) {
// Checks for the prime numbers
// having difference equal to2
if (prime[i] && prime[i - 2]) {
// Update count of triplets
cntTriplet[i] = cntTriplet[i - 1] + 1;
}
else {
cntTriplet[i] = cntTriplet[i - 1];
}
}
// Print the answer
cout << cntTriplet[N];
}
// Driver Code
int main()
{
long N = 7;
primeTriplet(N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
static int MAX = 1000001;
// Boolean array to
// mark prime numbers
static boolean[] prime = new boolean[MAX];
// To count the prime triplets
// having the sum of the first two
// numbers equal to the third element
static int[] cntTriplet = new int[MAX];
// Function to count prime triplets
// having sum of the first two elements
// equal to the third element
static void primeTriplet(int N)
{
// Sieve of Eratosthenes
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int i = 2; i * i <= N; i++)
{
if (prime[i])
{
for (int j = i * i; j <= N; j += i)
{
prime[j] = false;
}
}
}
for (int i = 3; i <= N; i++)
{
// Checks for the prime numbers
// having difference equal to2
if (prime[i] && prime[i - 2])
{
// Update count of triplets
cntTriplet[i] = cntTriplet[i - 1] + 1;
}
else
{
cntTriplet[i] = cntTriplet[i - 1];
}
}
// Print the answer
System.out.println(cntTriplet[N]);
}
// Driver Code
public static void main(String[] args)
{
int N = 7;
primeTriplet(N);
}
}
// This code is contributed by susmitakundugoaldagna.
Python3
# Python program for the above approach
# Function to count prime triplets
# having sum of the first two elements
# equal to the third element
def primeTriplet( N):
# Sieve of Eratosthenes
# Boolean array to
# mark prime numbers
prime = [True for i in range(1000001)]
# To count the prime triplets
# having the sum of the first two
# numbers equal to the third element
cntTriplet = [ 0 for i in range(1000001)]
prime[0] = prime[1] = False
i = 2
while i * i <= N:
if (prime[i]):
j = i * i
while j <= N:
prime[j] = False
j += i
i += 1
for i in range(3, N + 1):
# Checks for the prime numbers
# having difference equal to2
if (prime[i] and prime[i - 2]):
# Update count of triplets
cntTriplet[i] = cntTriplet[i - 1] + 1
else:
cntTriplet[i] = cntTriplet[i - 1]
# Print the answer
print(cntTriplet[N])
# Driver Code
N = 7
primeTriplet(N)
# This code is contributed by rohitsingh07052
C#
// C# Program to implement
// the above approach
using System;
class GFG
{
static int MAX = 1000001;
// Boolean array to
// mark prime numbers
static bool[] prime = new bool[MAX];
// To count the prime triplets
// having the sum of the first two
// numbers equal to the third element
static int[] cntTriplet = new int[MAX];
// Function to count prime triplets
// having sum of the first two elements
// equal to the third element
static void primeTriplet(int N)
{
// Sieve of Eratosthenes
for(int i = 0; i <= N; i++)
{
prime[i] = true;
}
prime[0] = prime[1] = false;
for (int i = 2; i * i <= N; i++)
{
if (prime[i])
{
for (int j = i * i; j <= N; j += i)
{
prime[j] = false;
}
}
}
for (int i = 3; i <= N; i++)
{
// Checks for the prime numbers
// having difference equal to2
if (prime[i] && prime[i - 2])
{
// Update count of triplets
cntTriplet[i] = cntTriplet[i - 1] + 1;
}
else
{
cntTriplet[i] = cntTriplet[i - 1];
}
}
// Print the answer
Console.WriteLine(cntTriplet[N]);
}
// Driver Code
public static void Main(String[] args)
{
int N = 7;
primeTriplet(N);
}
}
// This code is contributed by splevel62.
输出:
2
时间复杂度: O(N * log(log(N)))
辅助空间: O(N)