Prime visits problems

what is the problem with this piece of code

#include
using namespace std;
int countprime(long int a,long int b){
long int count=0;
long int i,j;
for(i=a;i<=b;i++){
int flag=0;
for(j=2;j<=i/2;j++){
if(i%j==0){
flag=1;
break;}
}
if(flag!=1&&i!=1)
count++;
}
return count;
}
int main() {
int n;
cin>>n;
int i,j,k;
for(i=0;i<n;i++){
long int a,b;
cin>>a>>b;
long int t=countprime(a,b);
cout<<t<<endl;
}
return 0;
}

Hi Pritika, since the constraints of this question are large values, try solving this problem by using sieve of eratosthenes. The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n and it will help prevent Time Limit Exceeded error. Let me know if there are further queries.

but i am not getting time limit exceed error , it is showing wrong ans for some testcases

Hey Pritika, there is a mistake in your code in line: 13 update your if check as if(flag!=1&&i!=1 && i!=0), because of this it is giving the wrong answer. And for larger inputs your code will not give any output as time limit will exceed, so to avoid that you are supposed to use sieve of Eratosthenes. For eg. check for this case
input:
5
0 100000
1 100000
2 100000
3 100000
4 100000

expected output :
9592
9592
9592
9591
9590