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 email@example.com. 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.
- 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
- Check if a prime number can be expressed as sum of two Prime Numbers
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Find if n can be written as product of k numbers
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Check if a number is Prime, Semi-Prime or Composite for very large numbers
- Sum of the digits of a number N written in all bases from 2 to N/2
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Sum of digits written in different bases from 2 to n-1
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
- Check if elements of array can be made equal by multiplying given prime numbers
- Check whether the sum of prime elements of the array is prime or not
- Prime numbers after prime P with sum S
- Print prime numbers with prime sum of digits in an array
- Sum of prime numbers without odd prime digits
- Count all prime numbers in a given range whose sum of digits is also prime
- Print the nearest prime number formed by adding prime numbers to N