Prime triangle: Difference between revisions

Content added Content deleted
(→‎{{header|Visual Basic .NET}}: Slight Simplification)
m (Added Perl)
Line 770: Line 770:
25.818809 seconds (249.58 M allocations: 22.295 GiB, 15.56% gc time)
25.818809 seconds (249.58 M allocations: 22.295 GiB, 15.56% gc time)
</pre>
</pre>

=={{header|Perl}}==
{{trans|Raku}}
{{libheader|ntheory}}
<lang perl>use strict;
use warnings;
use feature 'say';
use ntheory 'is_prime';
use List::MoreUtils qw(zip slideatatime);
use Algorithm::Combinatorics qw(permutations);

say '1 2';

my @count = (0, 0, 1);

for my $n (3..17) {
my @even_nums = grep { 0 == $_ % 2 } 2..$n-1;
my @odd_nums = grep { 1 == $_ % 2 } 3..$n-1;
for my $e (permutations [@even_nums]) {
next if $$e[0] == 8 or $$e[0] == 14;
my $nope = 0;
for my $o (permutations [@odd_nums]) {
my @list = zip @$e, @$o;
pop @list if ! defined $list[-1];
push @list, $n;
my $it = slideatatime(1, 2, @list);
while ( my @rr = $it->() ) {
last unless defined $rr[1];
$nope++ and last unless is_prime $rr[0]+$rr[1];
}
unless ($nope) {
say '1 ' . join ' ', @list unless $count[$n];
$count[$n]++;
}
$nope = 0;
}
}
}

say "\n" . join ' ', @count[2..$#count];</lang>
{{out}}
<pre>1 2
1 2 3
1 2 3 4
1 4 3 2 5
1 4 3 2 5 6
1 4 3 2 5 6 7
1 2 3 4 7 6 5 8
1 2 3 4 7 6 5 8 9
1 2 3 4 7 6 5 8 9 10
1 2 3 4 9 10 7 6 5 8 11
1 2 3 4 9 10 7 6 5 8 11 12
1 2 3 4 7 6 5 12 11 8 9 10 13
1 2 3 4 13 6 11 8 9 10 7 12 5 14
1 2 3 4 13 6 11 8 9 10 7 12 5 14 15
1 2 3 4 13 6 11 8 9 10 7 12 5 14 15 16
1 2 15 4 13 6 11 8 9 10 3 16 7 12 5 14 17

1 1 1 1 1 2 4 7 24 80 216 648 1304 3392 13808 59448</pre>


=={{header|Phix}}==
=={{header|Phix}}==