We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importsysdefsieve_of_eratosthenes(limit):# Create a boolean array "prime[0..n]" and initialize all entries as true.# A value in prime[i] will finally be false if i is Not a prime, else true.prime=[Truefor_inrange(limit+1)]p=2while(p*p<=limit):# If prime[p] is not changed, then it is a primeifprime[p]:# Update all multiples of pforiinrange(p*p,limit+1,p):prime[i]=Falsep+=1# Return all prime numbersreturn[pforpinrange(2,limit)ifprime[p]]# Function to find the nth prime numberdefnth_prime(n,primes_list):returnprimes_list[n-1]# Read the number of test casest=int(input().strip())# The upper limit for the 10,000th prime number is 104,729.# This is a known fact and can be used here for optimization.primes_list=sieve_of_eratosthenes(104729)fora0inrange(t):n=int(input().strip())# Print the nth prime numberprint(nth_prime(n,primes_list))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #7: 10001st prime
You are viewing a single comment's thread. Return to all comments →