Created
May 4, 2016 13:07
-
-
Save AlexDaniel/ce3c60e1701c378a3b115fbedea03ff5 to your computer and use it in GitHub Desktop.
Pointless benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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