Prime visits challenge

please check the code…please tell me why its not working for other test cases
#include
using namespace std;
int main() {
int N;
cin>>N;
for(int i=0;i<N;i++)
{
int a=0;
int b=0;
cin>>a>>b;
if(a==1)
{
a++;
}

int count=0;
int flag=1;
for(int j=a;j<=b;j++)
{
    
for(int i=2;i<j;i++)
{
    if(j%i!=0)
    {
       flag=1;

    }
    else
    {
        //cout<<" for u";
        flag=0;
        break;
    }
}
  if(flag==1)
 {
     //cout<<"hello"<<" ";
    count++;
  }
  else
  {
   //cout<<" aaja aaja";
  }
}
cout<<count<<endl;
}
return 0;

}

Hello @ashnagalhotra,

Always Share your code using Online Coding Blocks IDE.
The way you have shared it may introduce many run-time error to it.
STEPS:

  1. Paste it at https://ide.codingblocks.com/
  2. Save your code there
  3. Share the URL generted

Now, coming back to our code:
As, the value of a and b can be anything in the range [1,1000000].
For the cases like a=1 and b=1000000,
Your code will face TLE.

Reason:
Time complexity of your code is O(Nbb) which is very high.

Solution:
Rather use prime sieves method to solve this problem.

Hope, this would help.
Give a like if you are satisfied.

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.