J
Jeremy Fischer
Tom, Larry, Randall....
what do you make of this elegant snippet?
------------------------
#!/usr/local/bin/perl
$|++;
$p = [ 2,3,5,7,11 ]; # P not P
foreach $n (@$p) { # populate hash and array
$p{$n}++; # with first couple of primes
} # and set the candidate to the last
# prime plus one
# roughly 30 seconds
# p(2.3.5.7.11 = 2310)
# n.b.
# p() function
# $p->[] array
# $p->{} hash
$n = 13;
#($p->[-1]) +2;
while (scalar(@$p) < (2*3*5*7*11) ) {
if ( &j($n,$p) ) {
push (@$p, $n);
$p{$n}++;
}
$n++;
}
sub j {
my ($n,$primes) = @_;
foreach $p (@$primes ) {
if ( $n % $p == 0 ) {
return(0);
}
}
print "$n + ";
if ( $tab++ % 3 == 1 ) {
print "= \n ";
}
return(1);
}
sub p {
my ($n) = @_;
return ($p->[$n]);
# given n, return p(n)
}
sub n {
# given p, return
# which one (if prime)
# zero (if not)
}
sub pth {
my ( $th, $width) = @_;
my $sum = 0;
foreach $q ( ($th -$width) .. ($th +$ width) ) {
$sum += &p($q);
}
return ($sum);
}
what do you make of this elegant snippet?
------------------------
#!/usr/local/bin/perl
$|++;
$p = [ 2,3,5,7,11 ]; # P not P
foreach $n (@$p) { # populate hash and array
$p{$n}++; # with first couple of primes
} # and set the candidate to the last
# prime plus one
# roughly 30 seconds
# p(2.3.5.7.11 = 2310)
# n.b.
# p() function
# $p->[] array
# $p->{} hash
$n = 13;
#($p->[-1]) +2;
while (scalar(@$p) < (2*3*5*7*11) ) {
if ( &j($n,$p) ) {
push (@$p, $n);
$p{$n}++;
}
$n++;
}
sub j {
my ($n,$primes) = @_;
foreach $p (@$primes ) {
if ( $n % $p == 0 ) {
return(0);
}
}
print "$n + ";
if ( $tab++ % 3 == 1 ) {
print "= \n ";
}
return(1);
}
sub p {
my ($n) = @_;
return ($p->[$n]);
# given n, return p(n)
}
sub n {
# given p, return
# which one (if prime)
# zero (if not)
}
sub pth {
my ( $th, $width) = @_;
my $sum = 0;
foreach $q ( ($th -$width) .. ($th +$ width) ) {
$sum += &p($q);
}
return ($sum);
}