Last active
May 16, 2024 03:05
-
-
Save MasterDuke17/81fd08982738c65f2274e3bf0d09c7ea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
sub sieve($n) { | |
my @composite; | |
my $t; | |
loop ($t = 3; $t*$t <= $n; $t += 2) { | |
if (!@composite[$t]) { | |
loop (my $s = $t*$t; $s <= $n; $s += $t*2) | |
{ @composite[$s]++ } | |
} | |
} | |
my @primes = (2); | |
loop ($t = 3; $t <= $n; $t = $t + 2) { | |
@composite[$t] || push @primes, $t; | |
} | |
@primes; | |
} | |
my $N = 500000; | |
my $start = now; | |
my @primes = sieve($N); | |
my $end = now; | |
say @primes[*-1]; | |
say $end - $start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment