Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Prime numbers: addative property modulo p, where p is prime

Reply
Thread Tools

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

 
 
Jeremy Fischer
Guest
Posts: n/a
 
      01-16-2005
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);

}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[xsl] sort & modulo Tjerk Wolterink XML 3 04-22-2005 12:49 PM
Java ME modulo method ?? 900390d@acadiau.ca Java 1 05-19-2004 11:56 PM
Fast Division/Modulo Operation silentlights C Programming 8 04-23-2004 12:21 PM
Problems using modulo Griff Python 5 04-20-2004 12:20 PM
General modulo-question Christian Kruggel Java 1 07-07-2003 05:44 PM



Advertisments