Pierpont primes: Difference between revisions

Content added Content deleted
(julia example)
(→‎{{header|Perl 6}}: Rearrange a bit, move stuff out of the hot loop, cut runtime by ~40%)
Line 215: Line 215:
<lang perl6>use ntheory:from<Perl5> <is_prime>;
<lang perl6>use ntheory:from<Perl5> <is_prime>;


sub pierpont ($type is copy = 1) {
sub pierpont ($kind is copy = 1) {
fail "Unknown type: $kind. Must be one of 1 (default) or 2" if $kind !== 1|2;
gather {
my $po3 = 0;
fail "Unknown type: $type. Must be one of 1 (default) or 2" if $type !== 1|2;
take 2 if $type == 1;
my $add-one = 3;
my @iterators = [2,4,8 … *].iterator, [3,9,27 … *].iterator;
$type = -1 if $type == 2;
state $po3 = 0;
state $add-one = 3;
state @iterators = [2,4,8 … *].iterator, [3,9,27 … *].iterator;


gather {
take 2 if $kind == 1;
$kind = -1 if $kind == 2;
my @head = @iterators».pull-one;
my @head = @iterators».pull-one;


Line 231: Line 231:
@head[$key] = @iterators[$key].pull-one;
@head[$key] = @iterators[$key].pull-one;


take $min + $type if "{$min + $type}".&is_prime;
take $min + $kind if "{$min + $kind}".&is_prime;


if $min >= $add-one {
if $min >= $add-one {
++$po3;
++$po3;
@iterators.push: ([|((2,4,8).map: * * 3 ** $po3) … *]).iterator;
@iterators.push: ([|((2,4,8).map: * * 3 ** $po3) … *]).iterator;
@head[+@iterators - 1] = @iterators[+@iterators - 1].pull-one;
$add-one = @head[+@iterators - 1] = @iterators[+@iterators - 1].pull-one;
$add-one *= 3;
}
}
}
}
Line 251: Line 250:
say "\n250th Pierpont prime of the second kind: " ~ pierpont(2)[249];
say "\n250th Pierpont prime of the second kind: " ~ pierpont(2)[249];


# And the question absolutely noone was asking:
# And the question absolutely nobody was asking:


say "\n1000th Pierpont prime of the first kind:\n" ~ pierpont[999];
say "\n1000th Pierpont prime of the first kind:\n" ~ pierpont[999];