Goldbach's comet: Difference between revisions

Added Perl
(Goldbach's comet en FreeBASIC)
(Added Perl)
Line 466:
The value of G(1000000) is 5402
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<lang perl>se strict;
use warnings;
use feature 'say';
 
use List::Util 'max';
use ntheory 'is_prime';
 
sub table { my $t = shift() * (my $c = 1 + max map {length} @_); ( sprintf( ('%'.$c.'s')x@_, @_) ) =~ s/.{1,$t}\K/\n/gr }
 
sub G {
my($n) = @_;
scalar grep { is_prime($_) and is_prime($n - $_) } 2 .. $n/2;
}
 
my(@x,@y);
@x = map 2 * $_ + 4, 0..99;
push @y, G($_) for @x;
 
say $_ for table 10, @y;
printf "G $_: %d", G($_) for 1e6;</lang>
{{out}}
<pre> 1 1 1 2 1 2 2 2 2 3
3 3 2 3 2 4 4 2 3 4
3 4 5 4 3 5 3 4 6 3
5 6 2 5 6 5 5 7 4 5
8 5 4 9 4 5 7 3 6 8
5 6 8 6 7 10 6 6 12 4
5 10 3 7 9 6 5 8 7 8
11 6 5 12 4 8 11 5 8 10
5 6 13 9 6 11 7 7 14 6
8 13 5 8 11 7 9 13 8 9
 
G 1000000: 5402</pre>
 
=={{header|Phix}}==
2,392

edits