Skip to content

Instantly share code, notes, and snippets.

@mmcclimon
Created August 9, 2012 13:59
Show Gist options
  • Save mmcclimon/3304434 to your computer and use it in GitHub Desktop.
Save mmcclimon/3304434 to your computer and use it in GitHub Desktop.
A Perl implementation of the Sieve of Eratosthenes to find primes
sub sieve {
my $limit = shift || 1000;
my @list = map {$_ = 1} 0..$limit;
for my $i (2..sqrt($limit)) {
if ($list[$i]) {
for (my $j = $i**2; $j < @list; $j += $i) {
$list[$j] = 0;
}
}
}
my @primes;
for (2..$#list) {
push @primes, $_ if $list[$_];
}
@primes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment