In this problem, you will have to print the n-th prime number. The first few prime numbers are given…
Click here to read the complete problem statement.
If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.
sympy is an external module and thus is not supported by Toph.
You can use standard library functions and standard modules only.
It is the same for any other online judges.
@hjr265
I think the runtime for Python should be 5x
I have optimized my python code very much but even then it took nearly 3s to run it. Then if a noob coder try to implement sieve here, how can he/she do it?
Thus the runtime should be 5x.
def nth_prime_number(n):
prime_list = [2]
num = 3
while len(prime_list) < n:
for p in prime_list:
if num % p == 0:
break
else:
prime_list.append(num)
num += 2
return prime_list[-1]
My code works to print nth prime but it seems memory limit exceeded
n = int(input())
r = (n+1)**2
nth = n - 1
d = [x for x in range(2, r)]
non_prime = []
for i in range(2, r):
for x in d:
if i % x == 0 and x != 1 and x != i:
non_prime.append(i)
non_prime = list(set(non_prime))
prime_numbers = [x for x in d if x not in non_prime]
print(prime_numbers[nth])
Many people are having problem with this problem. Here is my simple solution.
#include<bits/stdc++.h>
using namespace std;
const int n=1e7; //0=is prime.
bool isPrime[n]; //1= is not prime. In a global variable, every element=0 by default
int main()
{
int k,counter=1;
cin>>k;
for (int i=3; i*i<=n; i+=2) //Loop running through only odd numbers
{
if (!isPrime[i])
{
for (int j=i*i; j<=n; j+=i)
{
isPrime[j]=1;
}
}
}
for (int i=3; i<=n; i+=2) //Loop running through only odd numbers
{
if (k==1) {cout<<2; break;}
if (!isPrime[i])
{
counter++;
if (k==counter) {cout<<i<<'\n'; break;}
}
}
return 0;
}