I am getting a tle in test case number 5 even though my code is running perfectly for all cases.
My solution:
#include
#include
using namespace std;
#define limit 1000001
bool self[limit+1];
bool mark[limit+1];
void seive(){
memset(mark,true,sizeof(mark));
for(int i=2;ii<limit;i++){
if(mark[i]){
for(int j=2i;j<limit;j+=i){
mark[j]=false;
}
}
}
}
void selfNum(){
memset(self,false,sizeof(self));
int x,sum;
for(int i=1;i<limit;i++){
x=sum=i;
while(x>0){
sum=sum+x%10;
x=x/10;
}
self[sum]=true;
}
}
int main()
{
seive();
selfNum();
int t;
cin>>t;
while(t–){
mark[0]=false;
mark[1]=false;
int m,n;
cin>>n>>m;
int counter=0;
for(int i=n;i<=m;i++){
if(mark[i]==true){
if(self[i]==false){
counter++;
}
}
}
cout<<counter<<endl;
}
return 0;
}