Doubt in Arrays

Prime Visits

#include<bits/stdc++.h>
#include
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{

 int a,b;
 cin>>a>>b;int cnt=0;int cnt1=0;
 for(int i=a;i<=b;i++)
 {
       int x = i;
       cnt=0;
       for(int j=2;j<=sqrt(x);j++)
       {
           if(x%j==0)
             cnt++;
       }
       if(cnt==0 && i>1)
          cnt1++;
       
 }
 cout<<cnt1<<endl;


}
return 0;

}

i was getting tle can you please help me how to optimize my code.

@dare_devil_007 The naive approach for generating the prime numbers will fail for large test cases in this problem. You have to try it using Sieve of Eratosthenes whose lecture videos are available in the Number Theory section of your course. Try this problem after referring that.

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.