1

Given a number n and integer k, check whether k prime number sums to n or not.

input 13 2
output: yes
explanation: 11+2 equals 13

since k is assumed to be any general integer, I don't know how to solve it. I thought to solve it by creating set of all prime number and looking for k number but even if k is as small as 5 we have to run 4 to 5 loop to do it. how to approach such problem, kindly looking for help,thanks. I tried initial code as:

#include<iostream>
#include<unordered_set>
#include<vector>
using namespace std;
bool is_prime(int n){
    bool flag =true;
    for(int i=2;i<n;i++){
        if(n%i==0 && n!=i){
            flag=false;
            break;
        }
    }
    if(flag){
        return true;
    }
    return false;
}
int main(){
    int n;cin>>n;
    int k;cin>>k;
    unordered_set<int>s;
    for(int i=2;i<n;i++){
        if(is_prime(i)){
            s.insert(i);
        }
    }
}
3
  • Which are the ranges of n and k? Commented Aug 5, 2021 at 11:19
  • @jarod42 nothing was mentioned, consider integer range in general Commented Aug 5, 2021 at 11:22
  • So brute-force, or even O(N+K) seems out of consideration. Have to find a mathematical formula. Commented Aug 5, 2021 at 12:11

1 Answer 1

6

This can be solved by assuming Goldbach's conjecture. Goldbach's conjecture says:

Any even integer is the sum of two prime numbers

We can exploit this to create the following rules:

  1. If n < 2k then NO (because 2 is the smallest prime)
  2. If k == 1 then YES IFF n is prime
  3. If n >= 2k and k == 2 THEN YES if n is even (Goldbach) , If n is odd then NO iff n-2 is not a prime number
  4. If n >= 2k and k >= 3 THEN Always YES:
    • When n is even, it can be expressed as 2 + ... + 2 + (n - 2 * (k - 2)),
      n - 2 * (k - 2) is also even and can be expressed as a sum of two primes (by Goldbach),
    • When n is odd, it can be expresses as 3 + 2 + ... + 2 + (n - 3 - 2 * (k - 3)),
      n - 3 - 2 * (k - 3) is even and can be expressed by sum of two primes (Goldbach).
Sign up to request clarification or add additional context in comments.

5 Comments

Goldbach's conjecture is still a conjecture because it is yet to be proven. Creating an algorithm based on it might not be a wise move even if the verified results are sufficient for the purpose.
@PalLaden - Well, if the algorithm fails, the OP has just found a counter-example to the Goldbach conjecture. That's a $1M prize or two right there. There's really no down-side.
@PalLaden it is proven for all integers less than 4×10^18, so for practical purposes it can be considered proven because that range is 10^9 larger than the range of C++ int
@christian sloper your approach seems visionary and you deduced it in a couple of minutes as well, It made me wonder, Thank you for the answer.
@BrettHale The problem is, the OP doesn't know the algorithm fails when it actually fails...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.