I’m currently working on the Sieve exercise:
When I run my function manually, I get the output expected based on my interpretation of the sieve.t file in the exercise. For example, consider the following:
is( # begin: 974945d8-8cd9-4f00-9463-7d813c7f17b7
find_primes(10),
[ 2, 3, 5, 7 ],
"find primes up to 10",
); # end: 974945d8-8cd9-4f00-9463-7d813c7f17b7
is( # begin: 2e2417b7-3f3a-452a-8594-b9af08af6d82
find_primes(13),
[ 2, 3, 5, 7, 11, 13 ],
"limit is prime",
); # end: 2e2417b7-3f3a-452a-8594-b9af08af6d82
Here is my function code (I’m sure it’s not optimal, but I’m more interested in why the results don’t pass) that I ran directly locally for testing:
sub find_primes ($limit) {
my @nums = (2..$limit);
say @nums;
for (my $i=0; $i< $limit; $i++) {
if(defined($nums[$i])) {
my $iter = $nums[$i];
say $iter . " is iter";
my $running_iter = $nums[$i];
say $running_iter . " is running_iter";
while($running_iter < $limit) {
@nums[$i + $running_iter] = undef;
$running_iter += $nums[$i];
}
}
}
my @result = grep( defined, @nums);
say \@result; #ARRAY
return @result;
}
say find_primes(13); # 23571113
say find_primes(10); # 2357
Initially, I wondered if this was some artifact of my local linux environment. However, NONE of the tests pass in the web IDE either, which implies to me I’m doing something very fundamentally incorrect.
I see that the array is formatted in a run-on fashion when I print it, but I didn’t see anything about required formatting in the exercise so I assumed returning the above array should satisfy the requirement. What’s even weirder is that the results show nonsensical values for my test results:
# Failed test 'no primes under two'
# at t/sieve.t line 9.
# +-----+---------+
# | GOT | CHECK |
# +-----+---------+
# | 0 | <ARRAY> |
# +-----+---------+
# Failed test 'find first prime'
# at t/sieve.t line 15.
# +-----+---------+
# | GOT | CHECK |
# +-----+---------+
# | 1 | <ARRAY> |
# +-----+---------+
Anyway, would appreciate being pointed in the right direction. I’m doggedly trying to learn perl
Thank you for your time