2 out of 3 test cases are working but the time limit of first is exceeding. I can’t figure out what to do.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int c=0;
int flag=0;
int a;
cin>>a;
int b;
cin>>b;
for (int i = a; i <= b; i++)
{
if (i == 1 || i == 0)
continue;
flag = 1;
for (int j = 2; j <=sqrt(i); ++j)
{
if (i % j == 0)
{
flag = 0;
break;
}
}
if (flag == 1)
++c;
}
cout<<c<<endl;
}
}