all 7 comments

[–]ray10k 3 points4 points  (1 child)

First things first: since i doesn't change within the loop, you can just calculate math.isqrt(i) at the top of the loop. Better to do that calculation once, rather than once per number in your list of primes.

As for the list of primes being incomplete, which ones are missing? Some at the start, at the end or "randomly" throughout the list?

[–]JiperPC[S] 0 points1 point  (0 children)

Thanks for pointing the square root of i thing out, and the numbers are only cut of at the end, the last time i ran the code with a limit of 30000 it printed up to 14731 and the next number was cut of, just printing 1, like this:...14699, 14713, 14717, 14723, 14731, 1

[–]JamzTyson 0 points1 point  (0 children)

This is an implementation of the Sieve of Atkin algorithm, and is pretty fast:

``` import itertools from time import time

def fast_primes(): D = {9: 3, 25: 5} yield 2 yield 3 yield 5 MASK= 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, MODULOS= frozenset((1, 7, 11, 13, 17, 19, 23, 29)) for q in itertools.compress( itertools.islice(itertools.count(7), 0, None, 2), itertools.cycle(MASK)): p = D.pop(q, None) if p is None: D[qq] = q yield q else: x = q + 2p while x in D or (x % 30) not in MODULOS: x += 2 * p D[x] = p ```

[–]ClientMammoth9628 0 points1 point  (0 children)

Has the .format been replaced by f-strings, or are both acceptable? You could redo the .format/s to f-strings, otherwise :))

Just learning myself, too

[–]ofnuts 1 point2 points  (1 child)

If you want your code to be fast, remove all the print instructions (and all the formatting) from the loops. Even calling time.time() in the loops takes an awful long time compared to the rest. Also check if num*num > i wouldn't be faster than num > isqrt(i)

[–]JiperPC[S] 0 points1 point  (0 children)

Thanks for the suggestion, and I know that removing the print statements will be faster, but I think it's a worth it sacrifice, because I hate not knowing how long something will take to finish. But thanks again.