Hello pvfki,
Your script has a number of problems:
- The forprimes function must be explicitly imported: e.g., use ntheory 'forprimes';.
- forprimes takes a block followed by either 1 or 2 integers.
- Within the block, the current prime is assigned to $_ ($p always remains 100).
- In the expression grep {!/$remove/} @candidates the regex matches any string containing the characters in $remove. So, e.g., if $remove is 1 then the regex will match 10. To fix this, you can add a beginning-of-string and an end-of-string assertion: grep {!/^$remove$/} .... However, it’s simpler to use != here: grep { $_ != $remove } @candidates.
- There is no real point in calling sieve2() twice. Since you don’t pass @candidates into the function, it will be set by the first call and unchanged by subsequent calls.
I think the following code is what you’re looking for:
use warnings;
use strict;
use ntheory 'forprimes';
use bigint;
my $n = 12;
my $c = 1;
my $k = 10;
my $l = 10;
my $p = 100;
my @candidates = (0 .. $l);
my $remove;
sieve2();
sub sieve2
{
forprimes
{
for my $i (0 .. $l)
{
if (($n * ($k + $i) + $c) % $_ == 0)
{
$remove = $i;
@candidates = grep { $_ != $remove } @candidates;
}
}
}
$p;
print join("\n", @candidates),"\n";
}
Output:
16:58 >perl 2012_SoPW.pl
3
5
6
9
10
16:58 >
Hope that helps,
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.