Skip to content

Instantly share code, notes, and snippets.

@lizmat
Created July 11, 2018 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizmat/b05d32486b1d74a4b69fcea13fe4b9d8 to your computer and use it in GitHub Desktop.
Save lizmat/b05d32486b1d74a4b69fcea13fe4b9d8 to your computer and use it in GitHub Desktop.
10% faster
# 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;
@a[1] = 0; # don't include 1
for (2 .. ($max div 2)) -> int $i {
my int $j = 2;
@a[$i * $j++] = 0 while $i * $j <= $max;
}
say @a.sum(:wrap);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment