The sieve of eratosthenes
Perl 6
#!/usr/bin/perl6
# Sieve of Eratosthenes in Perl 6
# Written by Robert Hoelz
class Sieve
{
has $limit;
has @primes;
submethod BUILD(*%args)
{
$limit = %args;
$limit-- unless $limit % 2;
@primes = 1..floor($!limit / 2);
@primes = @primes >>*<< 2;
@primes = @primes >>+<< 1;
}
method generate()
{
my $currentPrime = 3;
print '2 ';
until($currentPrime ** 2 > $limit) {
print "$currentPrime ";
@primes = grep {$_ % $currentPrime}, @primes;
$currentPrime = @primes[0];
}
}
method printRest()
{
say @primes.join(' ');
}
}
if @*ARGS > 0 {
my Sieve $sieve .= new(limit => @*ARGS[0]);
$sieve.generate();
$sieve.printRest();
}
stats
It is
Saturday May 17, 2008 11:04 am
This page served 40 times
This page last modified: April 29, 2008 2:10 pm
Your IP address is: 38.103.63.17
You are browsing using: CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
You are browsing from: United States.
badcomputer.org's uptime: 11:04:38 up 23 days, 11:46, 0 users, load average: 0.01, 0.07, 0.03
local
home | unix stuff | dir2ogg | sneetchalizer | wmainfo | q&d guide to permissions | q&d guide to tar and gzip | code | MS rant | browser shootout | linux & iAudio X5 | photos | music | programming poetry | sieve of Eratosthenes | plea | rain | suffer | archive | about | recipes | compaqr3000 | sitemap
search
credits
This page, and all pages on this site were created and are maintained by Darren Kirby using valid XHTML 1.0 and CSS, and are ©copyright 2002 - 2008. The Penguin image was created by Tukka, and is used by permission. Inspiration for the look of this site was provided by Eric A. Meyer's CSS gallery. This website runs on Gentoo Linux. It is served by Apache. PHP and MySQL hold together the backend.