Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

polettix (7564)

  (email not shown publicly)

Journal of polettix (7564)

Tuesday February 20, 2007
06:43 PM

The Sieve of Eratosthenes

[ #32452 ]
Translating a python function for a sieve of eratosthenes (a way to detect prime numbers), I've come up with this:

sub eratosthenes {
   my %D;
   my $q = 2;
   return sub {
      while (defined(my $p = delete $D{$q})) {
         my $x = $p + $q;
         $x += $p while exists $D{$x};
         $D{$x} = $p;
      } ## end while (defined(my $p = delete...
      $D{$q * $q} = $q;
      return $q++;
} ## end sub eratosthenes

It's nice, even if it tends to blow for increasing values (due to memory requirements).

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.