In [1]:
import numpy as np
In [2]:
def make_prime(U):
    is_prime = np.zeros(U,np.bool)
    is_prime[2] = 1
    is_prime[3::2] = 1
    M = int(U**.5)+1
    for p in range(3,M,2):
        if is_prime[p]:
            is_prime[p*p::p+p] = 0
    return is_prime, is_prime.nonzero()[0]
In [3]:
is_prime, primes = make_prime(100)
primes
Out[3]:
array([ 2,  3,  5,  7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
       61, 67, 71, 73, 79, 83, 89, 97], dtype=int64)
In [4]:
%time make_prime(10 ** 6)
%time make_prime(10 ** 7)
%time make_prime(10 ** 8)
%time make_prime(10 ** 9)
Wall time: 2.99 ms
Wall time: 32.9 ms
Wall time: 526 ms
Wall time: 5.96 s
Out[4]:
(array([False, False,  True, ..., False, False, False]),
 array([        2,         3,         5, ..., 999999893, 999999929,
        999999937], dtype=int64))