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);
}
}
}
nandk?O(N+K)seems out of consideration. Have to find a mathematical formula.