Prime numbers: addative property modulo p, where p is prime

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);

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top