This little snippet will print the first 'x' primes, with 'x' being a number supplied as the argument to the snippet (defaults to 10). It relies on Abigail-II's one-line prime number test and iterators as described in Dominus upcoming book.
#!/usr/bin/perl use warnings; use strict; sub NEXT ($) { $_[0]->() } sub prime_iterator { my $num = 0; sub { do { $num++; } until ( (1 x $num) !~ /^(11+)\1+$/); return $num; } } my $prime = prime_iterator; print NEXT $prime, "\n" for (1 .. shift || 10);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Prime Iterator
by Anonymous Monk on Oct 24, 2002 at 21:12 UTC | |
by Ovid (Cardinal) on Oct 24, 2002 at 22:35 UTC | |
Re: Prime Iterator
by gjb (Vicar) on Oct 25, 2002 at 13:15 UTC | |
Re: Prime Iterator
by greenback (Initiate) on Oct 25, 2002 at 17:49 UTC | |
Re: Prime Iterator
by Anonymous Monk on Jan 01, 2003 at 20:09 UTC |