#include
#include<math.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;
int arr[1000002]={0};
arr[0]=1;
arr[1]=1;
for(long int i=2; i<=sqrt(1000001);i++)
{
if(!arr[i])
{
for(long int j=i*i; j<=1000001; j+=i)
{
arr[j]=1;
}
}
}
for(long int i=2; i<=1000001&&n>0; i++)
{
if((n==1)&&(arr[i]==0))
{
cout<<i<<endl;
break;
n–;
}
else if(arr[i]==0)
n–;
}
}
return 0;
}
Why this code is not working, time limit is exceeding?
You need to use prime seive optimisation in your code as given in the online video lecture in number theory section as,
p[0]=p[1]=0;
for(long long int i=3;i<=N;i=i+2)
{
p[i]=1;
}
for(long long int i=3;i<=N;i=i+2)
{
if(p[i])
{
for(long long int j=ii;j<=N;j=j+2i)
{
p[j]=0;
}
}
}