#include
using namespace std;
int main() {
int t;
cin>>t;
while(t–)
{
int a,b;
cin>>a>>b;
int c=0;
for(int i=a;i<=b;i++)
{ int j;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
break;
}
if(j>i/2 && i!=1)
c++;
}
cout<<c<<endl;
}
return 0;
}
what is wrong in this code???
Why this is giving tle in test case 1,wrong ans in test case 2,only test case 3 is correct?
Hello @Avishek-Kumar-2445349249011561,
You are getting TLE because the logic you are using to check if a number is prime number, is brute force.
Thus, it is taking time more than required.
You must have heard of the sieve of Eratosthenes to compute prime numbers.
You have a video regarding the same in your course.
I would suggest you try this method as it is an efficient way of doing this.
Hope, this would help.
If you still face any issue, feel free to ask.