给定一个奇数,我们需要将其表示为最多三个质数之和。
例子 :
Input : 27
Output : 27 = 3 + 5 + 19
Input : 15
Output : 15 = 2 + 13
方法:在这里,我们使用哥德巴赫猜想来解决这个问题。它说任何偶数整数都可以表示为两个质数之和。
这里有三种情况:
1)当N是质数时,打印数字。
2)当(N-2)是质数时,打印2和N-2。
3)将N表示为3 +(N-3)。显然,N-3将是一个偶数(从另一个奇数中减去一个奇数会得出偶数)。因此,根据哥德巴赫猜想,它可以表示为两个素数之和。因此,打印3和其他两个质数。
C++
// CPP program to express N as sum of at-most
// three prime numbers.
#include
using namespace std;
// Function to check if a number is prime or not.
bool isPrime(int x)
{
if (x == 0 || x == 1)
return false;
for (int i = 2; i * i <= x; ++i)
if (x % i == 0)
return false;
return true;
}
// Prints at most three prime numbers whose
// sum is n.
void findPrimes(int n)
{
if (isPrime(n)) // CASE-I
cout << n << endl;
else if (isPrime(n - 2)) // CASE-II
cout << 2 << " " << n - 2 << endl;
else // CASE-III
{
cout << 3 << " ";
n = n - 3;
for (int i = 0; i < n; i++) {
if (isPrime(i) && isPrime(n - i)) {
cout << i << " " << (n - i);
break;
}
}
}
}
// Driver code
int main()
{
int n = 27;
findPrimes(n);
return 0;
}
Java
// Java program to express N as sum
// of at-most three prime numbers.
import java.util.*;
class GFG{
// Function to check if a
// number is prime or not.
public static boolean isPrime(int x)
{
if (x == 0 || x == 1)
return false;
for (int i = 2; i * i <= x; ++i)
if (x % i == 0)
return false;
return true;
}
// Prints at most three prime
// numbers whose sum is n.
public static void findPrimes(int n)
{
if (isPrime(n)) // CASE-I
System.out.print( n );
else if (isPrime(n - 2)) // CASE-II
System.out.print( 2 + " " +
(n - 2) );
else // CASE-III
{
System.out.print( 3 + " ");
n = n - 3;
for (int i = 0; i < n; i++) {
if (isPrime(i) && isPrime(n - i)) {
System.out.print( i + " " +
(n - i));
break;
}
}
}
}
// driver code
public static void main(String[] args)
{
int n = 27;
findPrimes(n);
}
}
// This code is contributed by rishabh_jain
Python3
# Python3 program to express N as
# sum of at-most three prime numbers
# Function to check if a number
# is prime or not.
def isPrime(x):
if(x == 0 or x == 1) :
return 0
i = 2
while i * i <= x :
if (x % i == 0) :
return 0
i = i + 1
return 1
# Prints at most three prime numbers
# whose sum is n.
def findPrimes(n) :
if (isPrime(n)):
# CASE-I
print(n, end = " ")
elif (isPrime(n - 2)) :
# CASE-II
print ("2", end = " ")
print (n - 2, end = " " )
else:
#CASE-III
print ( "3", end = " " )
n = n - 3
i = 0
while i < n :
if (isPrime(i) and isPrime(n - i)) :
print(i, end = " ")
print ((n - i), end = " ")
break
i = i + 1
# Driver Code
n = 27;
findPrimes(n);
# This code is contributed by rishabh_jain
C#
// C# program to express N as sum
// of at-most three prime numbers.
using System;
class GFG
{
// Function to check if a
// number is prime or not.
public static bool isPrime(int x)
{
if (x == 0 || x == 1)
return false;
for (int i = 2; i * i <= x; ++i)
if (x % i == 0)
return false;
return true;
}
// Prints at most three prime
// numbers whose sum is n.
public static void findPrimes(int n)
{
if (isPrime(n)) // CASE-I
Console.WriteLine( n );
else if (isPrime(n - 2)) // CASE-II
Console.Write( 2 + " " +
(n - 2) );
else // CASE-III
{
Console.Write( 3 + " ");
n = n - 3;
for (int i = 0; i < n; i++) {
if (isPrime(i) && isPrime(n - i))
{
Console.WriteLine( i + " " +
(n - i));
break;
}
}
}
}
// Driver code
public static void Main()
{
int n = 27;
findPrimes(n);
}
}
// This code is contributed by vt_m
PHP
Javascript
输出 :
3 5 19