Optimize Sieve of Eratosthenes for big numbers in Python I'm solving Euler exercise number 7 and trying to go a bit further on bigger primes.
It is too slow with big numbers. I've been reading ways to optimize the sieve, but I would like to ask people with more experience with this.
import time
from math import log, ceil

start = time.time()
primes = [2]
op_count = 0
limitN = 0
pr_count = 0

def primes_goto(prime_index):
    global primes
    global op_count
    global limitN
    global pr_count

    if prime_index &lt; 6:
        limitN = 100
    else:
        limitN = ceil(prime_index * (log(prime_index) + log(log(prime_index)))) #bound

    not_prime = set()

    while pr_count &lt; prime_index:
        for i in range(3, limitN, 2):
            if i in not_prime:
                continue

            for j in range(i*3, limitN, i*2):
                not_prime.add(j)

            primes.append(i)
            pr_count += 1

    return primes


ind = int(10001)
primes_goto(ind)
ind_prime = primes[ind-1]

end = time.time()


print(&quot;Prime number at posizion: {} = {}&quot;.format(ind, ind_prime))
print(&quot;Runtime: {}&quot;.format(end-start))
You can make this part a bit faster:
            for j in range(i*3, limitN, i*2):
                not_prime.add(j)

Better start at i*i, and better don't do your own loop. So it becomes:
            not_prime.update(range(i*i, limitN, i*2))