Skip to content

Instantly share code, notes, and snippets.

@dogbert17
Created August 27, 2021 13:21
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 dogbert17/9cb16b7a768daae1382fffab72b274de to your computer and use it in GitHub Desktop.
Save dogbert17/9cb16b7a768daae1382fffab72b274de to your computer and use it in GitHub Desktop.
Old program which is quite slow on new-disp
# Find the sum of all the primes below two million.
# real 0m22.322s
# user 0m22.272s
# sys 0m0.044s
# This is Rakudo version 2016.08.1-112-gbdd4691 built on MoarVM version 2016.08-32-ge52414d
use v6.c;
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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment