Avatar ·

Fast ways to find all prime numbers less than N

📁 Advanced Python Topics, question, numpy, AI

What algorithms exist for the fastest finding of all prime numbers less than N, besides the well-known ones:

And what fast implementations exist in Python (Vanilla, Numpy, etc.)?

def eratosthenes2(n):    multiples = set()    for i in range(2, n+1):        if i not in multiples:            yield i            multiples.update(range(i*i, n+1, i))

Avatar ·

I'll try to answer, since the question is interesting and I didn't know the reason myself.

In short: the reason is the presence of the magic method

If we use a dictionary as a key:

t = dict()d = {t: 1}

We get an error:

TypeError: unhashable type: 'dict'

But, actually, we would get the same error if we try to get the hash:

t = dict()print(hash(t))

Unhashable types lack the magic method:

Log in to leave an answer

Blogs