Prime visits problem

i have checked code there is no infinite loop issue but only one testcase is showing up timelimit error.
code is as :

#include
using namespace std;
int main() {
int n;
cin>>n;
while(n>0)
{
long long a;
long long b;
cin>>a;
cin>>b;
long long noc=0;
for(long long i=a;i<=b;i++ )
{
long long j;
for(j=2;j<i;j++)
{
if(i%j==0)
{
break;
}
}
if(j==i)
{
noc++;
}
}
cout<<noc<<endl;
n–;
}
return 0;
}

You code runs in o(n ^2) time complexity.

Try implementing using prime sieve(sieve of eratosthenes)
It would help u solve the question in O(n log(logn)) time complexity

Prateek Bhaiya has a video for sieve of eratosthenes please refer that. You`ll understand it in 1 go

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.