Skip to content

Instantly share code, notes, and snippets.

@dogbert17
Created July 11, 2018 10:57
Show Gist options
  • Save dogbert17/1a084fcee9db8eaad5cf50ae89838ca1 to your computer and use it in GitHub Desktop.
Save dogbert17/1a084fcee9db8eaad5cf50ae89838ca1 to your computer and use it in GitHub Desktop.
Small (?) example
# Find the sum of all the primes below two million.
use v6;
my int $max = 2_000_000;
# find all primes up to $max using The Sieve of Erathostenes
my int @a = (0..$max);
for (2..($max div 2)) -> int $i {
my int $j = 2;
@a[$i * $j++] = 1 while $i * $j <= $max;
}
# remove discarded numbers and store the rest in a set for ease of lookup
my @primes = @a.grep(-> int $num {$num > 1});
say @primes.sum;
@lizmat
Copy link

lizmat commented Jul 11, 2018

About 10% faster version, using .sum(:wrap): https://gist.github.com/lizmat/b05d32486b1d74a4b69fcea13fe4b9d8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment