Project Euler #7: 10001st prime

  • + 0 comments

    python version of the answer

    def prime_s(num):

    primes = [True]*(num+1)
    
    primes[0] = False
    primes[1] = False
    
    for i in range(2, int(num**0.5)+1):
        if primes[i]:
            for j in range(i*i, num+1, i):
                primes[j] = False
    primes = [i for i in range(num+1) if primes[i]]
    return primes
    

    def nth_prime(n, precomputed_primes): import math if n <= len(precomputed_primes): return precomputed_primes[n-1] else: limit = int(n*math.log(n)*1.5) if n > 5 else 15

        primes = prime_s(limit)
    
        while len(primes) < n:
            limit *= 2
            primes = prime_s(limit)
        return primes[n-1]
    

    precomputed_primes = prime_s(10**6) t = int(input().strip()) for a0 in range(t): n = int(input().strip())

    if n==1:
        print(2)
        continue
    
    result = nth_prime(n, precomputed_primes)
    print(result)