I enjoyed this, but I am a little stuck on how stuck the author is on yield. If one ends up creating a large list of prime numbers with this generator, why not just return a large list of prime numbers? The author clearly notes how optimizing Python code is "odd" when you can more easily switch languages, but languages like C/C++/Rust would not have pretty code because it does not have yield. It is obvious that retur…
> If one ends up creating a large list of prime numbers with this generator But one does not always end up creating a large list of prime numbers, do they? > why not just return a large list of prime numbers? Because that requires upfront knowledge of the filtering factor or absence thereof. A generator means you don't care, you generate an infinite sequence and the consumer is free to do whatever they want. > It is…
My favorite prime number generator
21–30 of 88 posts
Re: My favorite prime number generator
#22Re: My favorite prime number generator
#23I enjoyed this, but I am a little stuck on how stuck the author is on yield. If one ends up creating a large list of prime numbers with this generator, why not just return a large list of prime numbers? The author clearly notes how optimizing Python code is "odd" when you can more easily switch languages, but languages like C/C++/Rust would not have pretty code because it does not have yield. It is obvious that retur…
Re: My favorite prime number generator
#24Earlier quoted context omitted.
> If one ends up creating a large list of prime numbers with this generator But one does not always end up creating a large list of prime numbers, do they? > why not just return a large list of prime numbers? Because that requires upfront knowledge of the filtering factor or absence thereof. A generator means you don't care, you generate an infinite sequence and the consumer is free to do whatever they want. > It is…
"I can't try to rewrite it in C++ or Rust for now, due to the lack of generator support; the yield statement is what makes this code so nice and elegant, and alternative idioms are much less convenient."
Everything I replied to is the opinion you personally expressed.
And note that the author had already preempted your comment in the first place:
> When we want a list of all the primes below some known limit, gen_primes_upto is great, and performs fairly well. There are two issues with it, though:
> We have to know what the limit is ahead of time; this isn't always possible or convenient.
Re: My favorite prime number generator
#25I enjoyed this, but I am a little stuck on how stuck the author is on yield. If one ends up creating a large list of prime numbers with this generator, why not just return a large list of prime numbers? The author clearly notes how optimizing Python code is "odd" when you can more easily switch languages, but languages like C/C++/Rust would not have pretty code because it does not have yield. It is obvious that retur…
Try solving some Project Euler problems. You'll see the win pretty quickly when you have to search a large range for primes.
Re: My favorite prime number generator
#26 d = defaultdict(list)
# Magic
for i in range(2, 10000):
for y in i in d and d.pop(i) or [i]:
d[i+y] += (y,)
# Not sure how to print this in a succinct way
print(sorted(list(set().union(*d.values()))))Re: My favorite prime number generator
#27It basically uses the priority queue to store all the composites up to a certain point and then you can assume the next one is prime.
I had some (admittedly not well written) code for it too https://gist.github.com/weskerfoot/4699275
It can also incorporates the wheel factorization optimization mentioned in this article.
Re: My favorite prime number generator
#28The Sieve of Atkin is VERY fast at generating primes between 1 and n. It asymptotically speeds up the process of generating prime numbers. It has time complexity O(n/(log log n)) https://www.baeldung.com/cs/prime-number-algorithms
Re: My favorite prime number generator
#29The Genuine Sieve of Eratosthenes https://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
Re: My favorite prime number generator
#30In J generating prime numbers is simply p:21, that gives you 79 which is the 21st prime number. It is so fast, I wonder what the algorithm behind it is. [0]: https://code.jsoftware.com/wiki/Vocabulary/pco
Noted in the details section of the link you provided:
"Primality testing on numbers larger than (2^31) uses the probabilistic Miller-Rabin algorithm."