where should i optimize more ?
#include
#include
using namespace std;
int ithprime(long int n){
if(n==1){
return 2;
}
if(n==2){
return 3;
}
if(n==0){
return 1;
}
int counter=2;
int j=5;
while(counter!=n){
int sqt=sqrt(j);
if(sqt*sqt==j){
j+=2;
continue;
}
int flag=0;
for(int i=3;i<=sqt;i+=2){
if(j%i==0){
flag=1;
break;
}
}
if(flag==0){
counter++;
}
j+=2;
}
return j-2;
}
int main() {
long int n;
cin>>n;
cout<<ithprime(n);
}
Finding nth prime tle
Hi Yuvraj,
You should Sieve Technique (which is discussed in the Number Theory section) for this problem. Checking individually for each integer will definitely give you a TLE.
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.