给定一个偶数(大于2),请打印两个素数,它们的总和将等于给定数。可能有几种组合。仅打印第一对。
有趣的一点是,根据哥德巴赫的猜想,解决方案始终存在。
例子 :
Input: n = 74
Output: 3 71
Input : n = 1024
Output: 3 1021
Input: n = 66
Output: 5 61
Input: n = 9990
Output: 17 9973
这个想法是使用Eratosthenes筛子找到所有小于或等于给定数N的素数。一旦有了一个告诉所有素数的数组,我们就可以遍历该数组以找到具有给定总和的对。
C++
// C++ program to find a prime number pair whose
// sum is equal to given number
// C++ program to print super primes less than
// or equal to n.
#include
using namespace std;
// Generate all prime numbers less than n.
bool SieveOfEratosthenes(int n, bool isPrime[])
{
// Initialize all entries of boolean array
// as true. A value in isPrime[i] will finally
// be false if i is Not a prime, else true
// bool isPrime[n+1];
isPrime[0] = isPrime[1] = false;
for (int i=2; i<=n; i++)
isPrime[i] = true;
for (int p=2; p*p<=n; p++)
{
// If isPrime[p] is not changed, then it is
// a prime
if (isPrime[p] == true)
{
// Update all multiples of p
for (int i=p*p; i<=n; i += p)
isPrime[i] = false;
}
}
}
// Prints a prime pair with given sum
void findPrimePair(int n)
{
// Generating primes using Sieve
bool isPrime[n+1];
SieveOfEratosthenes(n, isPrime);
// Traversing all numbers to find first
// pair
for (int i=0; i
Java
// Java program to find a prime number pair whose
// sum is equal to given number
// Java program to print super primes less than
// or equal to n.
class GFG
{
// Generate all prime numbers less than n.
static boolean SieveOfEratosthenes(int n, boolean isPrime[])
{
// Initialize all entries of boolean
// array as true. A value in isPrime[i]
// will finally be false if i is Not a
// prime, else true bool isPrime[n+1];
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= n; i++)
isPrime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If isPrime[p] is not changed,
// then it is a prime
if (isPrime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
isPrime[i] = false;
}
}
return false;
}
// Prints a prime pair with given sum
static void findPrimePair(int n)
{
// Generating primes using Sieve
boolean isPrime[]=new boolean[n + 1];
SieveOfEratosthenes(n, isPrime);
// Traversing all numbers to find first
// pair
for (int i = 0; i < n; i++)
{
if (isPrime[i] && isPrime[n - i])
{
System.out.print(i + " " + (n - i));
return;
}
}
}
// Driver code
public static void main (String[] args)
{
int n = 74;
findPrimePair(n);
}
}
// This code is contributed by Anant Agarwal.
Python 3
# Python 3 program to find a prime number
# pair whose sum is equal to given number
# Python 3 program to print super primes
# less than or equal to n.
# Generate all prime numbers less than n.
def SieveOfEratosthenes(n, isPrime):
# Initialize all entries of boolean
# array as True. A value in isPrime[i]
# will finally be False if i is Not a
# prime, else True bool isPrime[n+1]
isPrime[0] = isPrime[1] = False
for i in range(2, n+1):
isPrime[i] = True
p = 2
while(p*p <= n):
# If isPrime[p] is not changed,
# then it is a prime
if (isPrime[p] == True):
# Update all multiples of p
i = p*p
while(i <= n):
isPrime[i] = False
i += p
p += 1
# Prints a prime pair with given sum
def findPrimePair(n):
# Generating primes using Sieve
isPrime = [0] * (n+1)
SieveOfEratosthenes(n, isPrime)
# Traversing all numbers to find
# first pair
for i in range(0, n):
if (isPrime[i] and isPrime[n - i]):
print(i,(n - i))
return
# Driven program
n = 74
findPrimePair(n)
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# program to find a prime number pair whose
// sum is equal to given number
// C# program to print super primes less than
// or equal to n.
using System;
class GFG
{
// Generate all prime numbers less than n.
static bool SieveOfEratosthenes(int n, bool []isPrime)
{
// Initialize all entries of boolean
// array as true. A value in isPrime[i]
// will finally be false if i is Not a
// prime, else true bool isPrime[n+1];
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= n; i++)
isPrime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If isPrime[p] is not changed,
// then it is a prime
if (isPrime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
isPrime[i] = false;
}
}
return false;
}
// Prints a prime pair with given sum
static void findPrimePair(int n)
{
// Generating primes using Sieve
bool []isPrime=new bool[n + 1];
SieveOfEratosthenes(n, isPrime);
// Traversing all numbers to find first
// pair
for (int i = 0; i < n; i++)
{
if (isPrime[i] && isPrime[n - i])
{
Console.Write(i + " " + (n - i));
return;
}
}
}
// Driver code
public static void Main ()
{
int n = 74;
findPrimePair(n);
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出:
3 71