给定两个数字N和K。我们需要确定“ N”是否可以写为“ K”素数的总和。
给定N <= 10 ^ 9
例子 :
Input : N = 10 K = 2
Output : Yes
10 can be written as 5 + 5
Input : N = 2 K = 2
Output : No
这个想法是使用戈德巴赫猜想的,该猜想说每个偶数整数(大于2)都可以表示为两个素数之和。
如果N = 2K且K = 1 :答案是肯定的,如果N是质数
如果N> = 2K且K = 2 :如果N是偶数,则答案为是(戈德巴赫猜想),如果N为奇数,则当N-2不是素数时答案为否,如果N-2是素数,则答案为Yes。质数。这是因为我们知道奇数+奇数=偶数和偶数+奇数=奇数。因此,当N为奇数且K = 2时,一个数必须为2,因为它是唯一的偶数质数,因此现在的答案取决于N-2是否为奇数。
如果N> = 2K并且K> = 3:答案将始终是。当N是偶数N – 2 *(K-2)也是偶数时,N – 2 *(K – 2)可以写成两个质数之和(哥德巴赫猜想),p和q可以写成2, 2…..K – 2次,p,q。当N为奇数时,N – 3 -2 *(K – 3)为偶数,因此可以将其写为两个质数p的和,q和N可以写为2、2…..K-3倍,3, ,
C++
// C++ implementation to check if N can be
// written as sum of k primes
#include
using namespace std;
// Checking if a number is prime or not
bool isprime(int x)
{
// check for numbers from 2 to sqrt(x)
// if it is divisible return false
for (int i = 2; i * i <= x; i++)
if (x % i == 0)
return false;
return true;
}
// Returns true if N can be written as sum
// of K primes
bool isSumOfKprimes(int N, int K)
{
// N < 2K directly return false
if (N < 2*K)
return false;
// If K = 1 return value depends on primality of N
if (K == 1)
return isprime(N);
if (K == 2)
{
// if N is even directly return true;
if (N % 2 == 0)
return true;
// If N is odd, then one prime must
// be 2. All other primes are odd
// and cannot have a pair sum as even.
return isprime(N - 2);
}
// If K >= 3 return true;
return true;
}
// Driver function
int main()
{
int n = 10, k = 2;
if (isSumOfKprimes (n, k))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
Java
// Java implementation to check if N can be
// written as sum of k primes
public class Prime
{
// Checking if a number is prime or not
static boolean isprime(int x)
{
// check for numbers from 2 to sqrt(x)
// if it is divisible return false
for (int i=2; i*i<=x; i++)
if (x%i == 0)
return false;
return true;
}
// Returns true if N can be written as sum
// of K primes
static boolean isSumOfKprimes(int N, int K)
{
// N < 2K directly return false
if (N < 2*K)
return false;
// If K = 1 return value depends on primality of N
if (K == 1)
return isprime(N);
if (K == 2)
{
// if N is even directly return true;
if (N%2 == 0)
return true;
// If N is odd, then one prime must
// be 2. All other primes are odd
// and cannot have a pair sum as even.
return isprime(N - 2);
}
// If K >= 3 return true;
return true;
}
public static void main (String[] args)
{
int n = 10, k = 2;
if (isSumOfKprimes (n, k))
System.out.print("Yes");
else
System.out.print("No");
}
}
// Contributed by Saket Kumar
Python3
# Python implementation to check
# if N can be written as sum of
# k primes
# Checking if a number is prime
# or not
def isprime(x):
# check for numbers from 2
# to sqrt(x) if it is divisible
# return false
i = 2
while(i * i <= x):
if (x % i == 0):
return 0
return 1
# Returns true if N can be written
# as sum of K primes
def isSumOfKprimes(N, K):
# N < 2K directly return false
if (N < 2 * K):
return 0
# If K = 1 return value depends
# on primality of N
if (K == 1):
return isprime(N)
if (K == 2):
# if N is even directly
# return true;
if (N % 2 == 0):
return 1
# If N is odd, then one
# prime must be 2. All
# other primes are odd
# and cannot have a pair
# sum as even.
return isprime(N - 2);
# If K >= 3 return true;
return 1
# Driver function
n = 10
k = 2
if (isSumOfKprimes (n, k)):
print ("Yes")
else:
print ("No")
# This code is Contributed by Sam007.
C#
// C# implementation to check if N can be
// written as sum of k primes
using System;
class GFG {
// Checking if a number is prime or not
static bool isprime(int x)
{
// check for numbers from 2 to sqrt(x)
// if it is divisible return false
for (int i = 2; i * i <= x; i++)
if (x % i == 0)
return false;
return true;
}
// Returns true if N can be written as sum
// of K primes
static bool isSumOfKprimes(int N, int K)
{
// N < 2K directly return false
if (N < 2 * K)
return false;
// If K = 1 return value depends on primality of N
if (K == 1)
return isprime(N);
if (K == 2)
{
// if N is even directly return true;
if (N % 2 == 0)
return true;
// If N is odd, then one prime must
// be 2. All other primes are odd
// and cannot have a pair sum as even.
return isprime(N - 2);
}
// If K >= 3 return true;
return true;
}
// Driver function
public static void Main ()
{
int n = 10, k = 2;
if (isSumOfKprimes (n, k))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Sam007
PHP
= 3 return true;
return true;
}
// Driver Code
$n = 10; $k = 2;
if (isSumOfKprimes ($n, $k))
echo "Yes";
else
echo"No" ;
// This code is contributed by vt
?>
Javascript
输出 :
Yes