There was 10th question in project eular.
The problem is to find the sum of all prime numbers not greater than N.
My soln for the problem is :
int solve(int n){
bool check[n+1];
for(int i=0;i<=n;i++){
check[i]=true;
}
for(int i=2;i*i<=n;i++){
if(check[i]){
for(int j=i*i;j<=n;j+=i){
check[j]=false;
}
}
}
int sum=0;
for(int i=2;i<=n;i++){
if(check[i]){
sum+=i;
}
}
return sum;
}
Still the problem was not optimized enough as i was getting ' termination due to timeout ' message.
How can i optimize this code better.
The constraints are :
1<= T <= 10^4 ( T is no. of test cases )
1<= N <= 10^6
You can try it yourself here