use strict; use warnings; use 5.010; my @n; my @p; sub mk { my $l = shift; no warnings "redefine"; *is=eval 'sub { ' . join(' && ', map('$_[0] % '.$_, grep $_*$_ <= +$n[-1], @p), <<'EOCODE'); ($_[0] <= $l ? (push(@p, $_[0]), say($_[0])) : (unshift(@n, $_[0]), mk($p[-1]**2)) ) } EOCODE } @n=2..(shift//200); mk(2); is(shift @n) while @n;

Replies are listed 'Best First'.
Re: number sifter
by grinder (Bishop) on Feb 21, 2008 at 09:57 UTC

    Nice.

    NB: It craps out (on Win32 at least) after 4870843 with "The exception unknown software exception occurred in the application".

    On FreeBSD it manages to squeak past this figure and crank out subsequent numbers a couple per second. Hmmm.. ah yes, the technique uses quite a bit of memory :)

    • another intruder with the mooring in the heart of the Perl