Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

•Re: Sieve of Eratosthenes with closures

by merlyn (Sage)
on Jul 20, 2003 at 23:07 UTC ( [id://276108]=note: print w/replies, xml ) Need Help??


in reply to Sieve of Eratosthenes with closures

Here's a slightly more direct implementation of that:
use strict; $|++; my $generator = do { my $num = 1; sub { ++$num } }; while (1) { my $prime = $generator->(); print "$prime\n"; ## don't let any multiple of these escape: my $oldgenerator = $generator; $generator = sub { { my $next_prime = $oldgenerator->(); redo if $next_prime % $prime == 0; return $next_prime; } }; }

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-28 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found