Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Put a debugging statement in and you'll verify that the dictionary only contains primes up to the square root of the one just generated.

To do that it recursively contains a second prime generator that only produces up to the square root. Which contains one that produces up to the fourth root. And so on until you get down to 3. The work and memory for those recursive generators is a rounding error compared to the initial prime generator. The work and memory saved by not keeping unnecessary primes is a significant win.



Oh, thanks for the explanation, I get how it works now. I hadn't seen this trick for an unbounded prime sieve before, and it's a nice idea.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: