给定一个正整数N ,任务是找到所有素数三元组{P,Q,R} ,使得P = R – Q且P , Q和R小于N。
例子:
Input: N = 8
Output:
2 3 5
2 5 7
Explanation:
The only 2 prime triplets satisfying the given conditions are:
- {2, 3, 5}: P = 2, Q = 3, R = 5. Therefore, P, Q and R are prime numbers and P = R – Q.
- {2, 5, 7}: P = 2, Q = 5, R = 7. Therefore, P, Q and R are prime numbers and P = R – Q.
Input: N = 5
Output: 2 3 5
方法:可以根据以下观察结果解决给定问题:
- 通过重新排列给定的方程,可以观察到P + Q = R ,并且两个奇数之和是偶数,一个奇数和一个偶数的和是奇数。
- 因为只有一个偶数素数,即2 。令P为奇质数,而Q为奇质数,则R永远不可能是质数,因此有必要使P始终为2 ,它是偶数质数,而Q为奇质数,则R应为奇质数。因此,有必要找到质数Q ,使得Q> 2且R = P + Q≤N(其中P = 2),并且R应为质数。
请按照以下步骤解决问题:
- 使用Eratosthenes筛子对[1,N]范围内的所有素数进行预计算。
- 初始化向量的向量,例如V ,以存储所有结果三元组。
- 使用变量i遍历[3,N]范围。如果i和(i + 2)是素数且2 + i≤N ,则将当前的三元组存储在向量V中。
- 完成上述步骤后,打印存储在V []中的所有三胞胎。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Stores 1 and 0 at indices which
// are prime and non-prime respectively
bool prime[100000];
// Function to find all prime
// numbers from the range [0, N]
void SieveOfEratosthenes(int n)
{
// Consider all numbers to prime initially
memset(prime, true, sizeof(prime));
// Iterate over the range [2, sqrt(N)]
for (int p = 2; p * p <= n; p++) {
// If p is a prime
if (prime[p] == true) {
// Update all tultiples
// of p as false
for (int i = p * p;
i <= n; i += p) {
prime[i] = false;
}
}
}
}
// Function to find all prime triplets
// satisfying the given conditions
void findTriplets(int N)
{
// Generate all primes up to N
SieveOfEratosthenes(N);
// Stores the triplets
vector > V;
// Iterate over the range [3, N]
for (int i = 3; i <= N; i++) {
// Check for the condition
if (2 + i <= N && prime[i]
&& prime[2 + i]) {
// Store the triplets
V.push_back({ 2, i, i + 2 });
}
}
// Print all the stored triplets
for (int i = 0; i < V.size(); i++) {
cout << V[i][0] << " "
<< V[i][1] << " "
<< V[i][2] << "\n";
}
}
// Driver Code
int main()
{
int N = 8;
findTriplets(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Stores 1 and 0 at indices which
// are prime and non-prime respectively
static boolean[] prime = new boolean[100000];
static void initialize()
{
for(int i = 0; i < 100000; i++)
prime[i] = true;
}
// Function to find all prime
// numbers from the range [0, N]
static void SieveOfEratosthenes(int n)
{
// Iterate over the range [2, sqrt(N)]
for(int p = 2; p * p <= n; p++)
{
// If p is a prime
if (prime[p] == true)
{
// Update all tultiples
// of p as false
for(int i = p * p; i <= n; i += p)
{
prime[i] = false;
}
}
}
}
// Function to find all prime triplets
// satisfying the given conditions
static void findTriplets(int N)
{
// Generate all primes up to N
SieveOfEratosthenes(N);
// Stores the triplets
ArrayList> V = new ArrayList>();
// List > V = new List>();
// Iterate over the range [3, N]
for(int i = 3; i <= N; i++)
{
// Check for the condition
if (2 + i <= N && prime[i] && prime[2 + i])
{
// Store the triplets
ArrayList a1 = new ArrayList();
a1.add(2);
a1.add(i);
a1.add(i + 2);
V.add(a1);
}
}
// Print all the stored triplets
for(int i = 0; i < V.size(); i++)
{
System.out.println(V.get(i).get(0) + " " +
V.get(i).get(1) + " " +
V.get(i).get(2));
}
}
// Driver Code
public static void main(String args[])
{
initialize();
int N = 8;
findTriplets(N);
}
}
// This code is contributed by ipg2016107
Python3
# Python3 program for the above approach
from math import sqrt
# Stores 1 and 0 at indices which
# are prime and non-prime respectively
prime = [True for i in range(100000)]
# Function to find all prime
# numbers from the range [0, N]
def SieveOfEratosthenes(n):
# Iterate over the range [2, sqrt(N)]
for p in range(2, int(sqrt(n)) + 1, 1):
# If p is a prime
if (prime[p] == True):
# Update all tultiples
# of p as false
for i in range(p * p, n + 1, p):
prime[i] = False
# Function to find all prime triplets
# satisfying the given conditions
def findTriplets(N):
# Generate all primes up to N
SieveOfEratosthenes(N)
# Stores the triplets
V = []
# Iterate over the range [3, N]
for i in range(3, N + 1, 1):
# Check for the condition
if (2 + i <= N and prime[i] and prime[2 + i]):
# Store the triplets
V.append([2, i, i + 2])
# Print all the stored triplets
for i in range(len(V)):
print(V[i][0], V[i][1], V[i][2])
# Driver Code
if __name__ == '__main__':
N = 8
findTriplets(N)
# This code is contributed by bgangwar59.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Stores 1 and 0 at indices which
// are prime and non-prime respectively
static bool[] prime = new bool[100000];
static void initialize()
{
for(int i = 0; i < 100000; i++)
prime[i] = true;
}
// Function to find all prime
// numbers from the range [0, N]
static void SieveOfEratosthenes(int n)
{
// Iterate over the range [2, sqrt(N)]
for(int p = 2; p * p <= n; p++)
{
// If p is a prime
if (prime[p] == true)
{
// Update all tultiples
// of p as false
for(int i = p * p; i <= n; i += p)
{
prime[i] = false;
}
}
}
}
// Function to find all prime triplets
// satisfying the given conditions
static void findTriplets(int N)
{
// Generate all primes up to N
SieveOfEratosthenes(N);
// Stores the triplets
List> V = new List>();
// Iterate over the range [3, N]
for(int i = 3; i <= N; i++)
{
// Check for the condition
if (2 + i <= N && prime[i] ==
true && prime[2 + i])
{
// Store the triplets
List a1 = new List();
a1.Add(2);
a1.Add(i);
a1.Add(i + 2);
V.Add(a1);
}
}
// Print all the stored triplets
for(int i = 0; i < V.Count; i++)
{
Console.WriteLine(V[i][0] + " " +
V[i][1] + " " +
V[i][2]);
}
}
// Driver Code
public static void Main()
{
initialize();
int N = 8;
findTriplets(N);
}
}
// This code is contributed by SURENDRA_GANGWAR
输出:
2 3 5
2 5 7
时间复杂度: O(N * log(log(N)))
辅助空间: O(1)