Many years ago, I discovered a short and clear Python code for a prime sieve function that generates an infinite sequence of prime numbers. The code is based on the Sieve of Eratosthenes, an ancient Greek algorithm for finding primes efficiently. The key to this algorithm is a map called D, which keeps track of primes encountered so far. With this code, there’s no need to know the limit ahead of time, and its memory usage is reasonable. However, the code can be optimized further to make it faster. There are also other algorithms, like the segmented sieve of Eratosthenes, that use mathematical tricks to generate primes more efficiently. While optimizing Python code can be helpful, switching to a different language like Go can provide even greater benefits.

https://eli.thegreenplace.net/2023/my-favorite-prime-number-generator/