# N-th Prime

i’m getting WA on test 3
#include<bits/stdc++.h>
using namespace std;
const int N = 500000 ;
long long a[N];
int main(){
a[0] = 2;
a[1] = 3;
long long n,s,i,j=1;
for(n=4;n<=N;n++){
for(i=2;i<=sqrt(n);i++){
s = n%i;
if(s==0)
break;
}
if(s!=0){
j++;
a[j] = n;
}
}
long long k;
cin >> k;
cout << a[k-1] << endl;
return 0;
}

Need help to find out mistake

``````mxn=int(5e5)
vec=[0]*(mxn+1)

for i in range(2, len(vec)):
if vec[i]: continue
if i*i>mxn: break
for j in range(i*i, len(vec), i):
if vec[j]: continue
vec[j]=1

n=int(input())
c=0
for i in range(2, len(vec)):
if vec[i]: continue
c+=1
if c==n: break

print(i)
``````

Here is my code. Why is it showing CPU Limit Exceeded? How can it be optimised?

``````#include<bits/stdc++.h>
using namespace std;

bool isprime(int x){
bool k=true;
for(int i=7;i*i<=x;i+=30){
if(k%i==0 || k%(i+4)==0 || k%(i+6)==0 || k%(i+10)==0 || k%(i+12)==0 || k%(i+16)==0 || k%(i+22)==0 || k%(i+24)==0) {
k=false;
break;
}
}
return k;
}

int main (){
ios_base::sync_with_stdio(false);
cin.tie(NULL);

long n;
cin >> n;
if(n<4){
if(n==1) cout << 2;
if(n==2) cout << 3;
if(n==3) cout << 5;
}

long prime=0, s=7;
n-=3;
while(n!=0){
if(isprime(s)){
prime = s;
n--;
}
if(n!=0 && isprime(s+4)){
prime = s+4;
n--;
}
if(n!=0 && isprime(s+6)){
prime = s+6;
n--;
}
if(n!=0 && isprime(s+10)){
prime = s+10;
n--;
}
if(n!=0 && isprime(s+12)){
prime = s+12;
n--;
}
if(n!=0 && isprime(s+16)){
prime = s+16;
n--;
}
if(n!=0 && isprime(s+22)){
prime = s+22;
n--;
}
if(n!=0 && isprime(s+24)){
prime = s+24;
n--;
}
s+=30;
}

cout << prime;
return 0;

}
``````
``````from math import sqrt

def isPrime(x):
if x == 2:
return True
if x < 2 or x % 2 == 0:
return False
i = 3
while i < int(sqrt(x)):
if x % i == 0:
return False
i += 2
return True

def nThPrime(n):
result=0
if n<1: return result
if n==1: return 2
count = 1
j = 3
while count != n:
if isPrime(j):
count += 1
result=j
j += 2
return result

n = int(input())
print(nThPrime(n))

``````

I think, I am the person that you were talking about😞. can you help me?
How do I make it more efficient?

Can anyone tell me, how can I solve it in python?
I have done it in c with the “Sieve of Eratosthenes” but failed in python!