Given two numbers N and K. We need to find out if ‘N’ can be written as sum of ‘K’ prime numbers.
Given N <= 10^9
Input : N = 10 K = 2 Output : Yes 10 can be written as 5 + 5 Input : N = 2 K = 2 Output : No
The idea is to use Goldbach’s conjecture which says that every even integer (greater than 2) can be expressed as sum of two primes.
If the N = 2K and K = 1 : the answer will be Yes iff N is a prime number
If N >= 2K and K = 2 : If N is an even number answer will be Yes(Goldbach’s conjecture) and if N is odd answer will be No if N-2 is not a prime number and Yes if N-2 is a prime number. This is because we know odd + odd = even and even + odd = odd. So when N is odd, and K = 2 one number must be 2 as it is the only even prime number so now the answer depends whether N-2 is odd or not.
If N >= 2K and K >= 3 : Answer will always be Yes. When N is even N – 2*(K-2) is also even so N – 2*(K – 2) can be written as sum of two prime numbers (Goldbach’s conjecture) p, q and N can be written as 2, 2 …..K – 2 times, p, q. When N is odd N – 3 -2*(K – 3) is even so it can be written as sum of two prime numbers p, q and N can be written as 2, 2 …..K-3 times, 3, p, q
This article is contributed by Ayush Jha If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Check if a prime number can be expressed as sum of two Prime Numbers
- Find the prime numbers which can written as sum of most consecutive primes
- Check if a number can be written as sum of three consecutive integers
- Print the nearest prime number formed by adding prime numbers to N
- Check if two numbers are co-prime or not
- Check whether the given numbers are Cousin prime or not
- Check if each element of the given array is the product of exactly K prime numbers
- Check if product of array containing prime numbers is a perfect square
- Check if elements of array can be made equal by multiplying given prime numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Check if a number is divisible by all prime divisors of another number
- Represent a number as a sum of maximum possible number of Prime Numbers
- Check if a number is a Pythagorean Prime or not
- Check if a number is Quartan Prime or not
- Check whether the given number is Wagstaff prime or not
- Check if the first and last digit of number N is prime and their sum is less than K
- Check whether a number is circular prime or not
- Check if a number is Full Prime
- Check if N is a Balanced Prime number or not