Created
August 9, 2012 13:59
-
-
Save mmcclimon/3304434 to your computer and use it in GitHub Desktop.
A Perl implementation of the Sieve of Eratosthenes to find primes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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