Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re^5: Challenge: Another Infinite Lazy List

by tall_man (Parson)
on Mar 19, 2005 at 00:38 UTC ( #440837=note: print w/replies, xml ) Need Help??

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://440837]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2020-10-29 06:02 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (269 votes). Check out past polls.