http://www.perlmonks.org?node_id=440837


in reply to Re^4: Challenge: Another Infinite Lazy List
in thread Challenge: Another Infinite Lazy List

There are a couple of advantages of the lazy list form over grep.

1) You can get an exact number of values out of the sequence without having to know the maximum number range in advance. For example, your sample code produces only 36 values, not the expected 50. You would have to feed it a range up to 273 to get a full 50.

2) You could extract and print values in a loop without building a large result array (a map in void context could do the same, I suppose).

My estimates for timing were rough back-of-the-envelope calculations, so I'm not surprised that they are a bit off. There are a number of things I didn't take into account, such as the overlaps where more than one prime is a factor, and the fact that more numbers are eliminated with more factors, so you have to search bigger integers to finish the list. Thanks for your timing tests.

Math::Pari is a highly optimized math engine, so I'm not too surpised that single gcd checks beat multiple merges. But if you start with primes at 101 and above instead, the merge algorithm will win.

  • Comment on Re^5: Challenge: Another Infinite Lazy List