How to decrease time limit ass test case is showing time limit 5sec

#include
using namespace std;

 int calc(int n){
     int k,sum = 0;
   for(k =1;k<=n;k++){
        int j;
      for(j=2;j<n;j++){
          if(k%j==0){
            break;
          }
      }
      if(j==k){
        sum = sum+j;
   }
   }
   return sum;
 }

int main(){
int n;
cin>>n;
int a[n];
int i;
for( i=0;i<n;i++){
cin>>a[i];
}

 for( i=0;i<n;i++){
        if(a[i]==1){
            cout<<"2"<<endl;
        }
        else{
    cout<<calc(a[i]) +1<<endl;
}
 }

return 0;

}

Hey Ankush, please mention the problem’s exact name here and copy your code on ide, save it and share that link also.

https://online.codingblocks.com/player/7545/content/4702?s=1435 this que

this is code https://ide.codingblocks.com/s/51258

prateek love candy is the que in this my code is showing run time error

Hey Ankush, to reduce the time complexity you are supposed to use Sieve of Eratosthenes for this problem.

for checking prime my loop must go upto which number in line no 9 in my code https://ide.codingblocks.com/s/51790

terminating condition should be j*j<=n.

Hey Ankush, as you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “Ask Doubt” section, when your doubt is resolved.