Skip to content

Instantly share code, notes, and snippets.

@AlexDaniel
Created May 4, 2016 13:07
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 AlexDaniel/ce3c60e1701c378a3b115fbedea03ff5 to your computer and use it in GitHub Desktop.
Save AlexDaniel/ce3c60e1701c378a3b115fbedea03ff5 to your computer and use it in GitHub Desktop.
Pointless benchmark
#!/usr/bin/env perl6
sub get_primes(int $n) {
return () if $n < 2;
return (2) if $n == 2;
my int @s = ();
loop (my int $i = 3; $i <= $n; $i+=2) {
@s.push($i);
}
my int $mroot = Int(sqrt($n));
my int $half = @s.elems;
$i = 0;
my int $m = 3;
loop ($i = 0; $m <= $mroot; $i++) {
if @s[$i] {
my int $j = ($m*$m - 3) div 2;
@s[$j] = 0;
while ($j < $half) {
@s[$j] = 0;
$j += $m;
}
}
$m = 2 * ($i + 1) + 3;
}
@s.unshift: 2;
return @s.grep: {$_};
}
for ^10 {
my @res = get_primes(10000000);
put "Found {@res.elems} prime numbers.";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment