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

Discussion in 'Perl Misc' started by Jeremy Fischer, Jan 16, 2005.

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

    }
     
    Jeremy Fischer, Jan 16, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Luschny

    The tiger sieves prime numbers - fast!

    Peter Luschny, Nov 18, 2004, in forum: Java
    Replies:
    0
    Views:
    383
    Peter Luschny
    Nov 18, 2004
  2. Chris

    Prime Numbers

    Chris, Oct 31, 2003, in forum: C++
    Replies:
    4
    Views:
    600
    Josephine Schafer
    Oct 31, 2003
  3. AshifToday

    Composite and Prime Numbers

    AshifToday, May 22, 2005, in forum: C++
    Replies:
    0
    Views:
    2,781
    AshifToday
    May 22, 2005
  4. Tim Churches

    Safe prime numbers in Python

    Tim Churches, Jan 14, 2004, in forum: Python
    Replies:
    1
    Views:
    553
    Paul Rubin
    Jan 14, 2004
  5. Skip Montanaro

    Re: Safe prime numbers in Python

    Skip Montanaro, Jan 14, 2004, in forum: Python
    Replies:
    5
    Views:
    1,157
    Paul Rubin
    Jan 15, 2004
Loading...

Share This Page