I am able to get the first two tests of Sieve of Eratosthenes to pass, but after that things start to get…strange.
Here is my completed code:
package Sieve;
use v5.38;
use Exporter qw<import>;
our @EXPORT_OK = qw<find_primes>;
our (@primes, @marked, $limit);
sub find_primes ($limit) {
our @array = (2..$limit);
for my $i (@array) {
next if grep { $_ == $i } @marked;
push(@primes, $i);
my $composite = $i;
while ($composite <= $limit) {
$composite = $composite + $i;
push(@marked, $composite);
}
}
return \@primes;
}
Here is test 3…why is the number 2 not included in the check? Then it really gets weird.
Test 5, my output does this strange loop a couple of times before it finally gets going. I have tested this in my perl interpreter locally in Ubuntu and this issue does not occur.
Am I crazy?