Skip to content

Instantly share code, notes, and snippets.

@Timbus
Created May 3, 2017 10:44
Show Gist options
  • Save Timbus/2965b4f3c5ff29c165b56a13f2fbc668 to your computer and use it in GitHub Desktop.
Save Timbus/2965b4f3c5ff29c165b56a13f2fbc668 to your computer and use it in GitHub Desktop.
*In angry Megatron voice*: PRIME!!!
sub primes-sieve3(Int $max) {
my int @sieve;
my int $effective-max = ($max-1) div 2;
lazy gather for 1 .. $effective-max -> int $i {
FIRST take 2;
next if @sieve[$i];
my int $prime = 2 * $i + 1;
take $prime;
my $fill-range = ($effective-max - $i) div $prime;
for 1 .. $fill-range -> int $j {
@sieve[$prime * $j + $i] = 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment