Infinite prime generator
This is a performance issue:


primes_below_sqrt = {i for i in prime_set if i &lt;= sqrt(x)}



By using a set comprehension instead of a generator expression, you are actually making a copy of the subset.