Doubt on T-prime number

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i=2,ques;
int count;
cin>>ques;
for(int j=0;j<ques;j++)
{
cin>>n;
count=2;
for(i=2;i<n-1;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==3)
{
cout<<“YES”<<endl;
}
else
{
cout<<“NO”<<endl;
}
}
}

sir please help me to run this efficiently as i am getting timelimit error in hacker block . A s i had solved this after watching prime number basic video, which was present in fundamentals 2

Hey @manikantahariharan434 Currenty its taking you O(n) to check if the given number is prime or not, but we know that after square root of a number only one factor of a number exists that is the number itself, So you
can run the loop till sqrt(n) instead of n-1 and check if the number is prime in O(sqrt(n)).

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.