#Skip to menu

10001st prime

First read the problem description.

A simple Sieve of Eratosthenes.

import array

def primes_upto(N):
    S = array.array('B')
    S.extend(0 for x in range((N >> 3) + 1))
    def set_bit(i):
        S[i>>3] |= 1<<(i&7)
    def is_bit_set(i):
        return S[i>>3]&(1<<(i&7))  
    p = 2
    for p in range(p, N+1):
        if not is_bit_set(p):
            yield p
            k = 0
            while True:
                i = p*p + k*p
                if i > N:
                    break
                set_bit(i)
                k += 1

next(p for i,p in enumerate(primes_upto(1_000_000)) if i+1==10001)
104743

Source code of the solution(s):