http://qs321.pair.com?node_id=656028


in reply to Prime Numbers

One of the fastest ways would be to use a math library like PARI. It uses very good primality tests internally. I don't have it installed right now, but you should be able to use:
use Math::Pari 'isprime'; my $num = shift || '12345678901'; print isprime($num) ? "$num is prime!\n" : "$num is composite!\n";
Even if you are looking for the practice of implementing a nice primality test by hand, pari would be a good tool, as it supports all sorts of abstract algebra things (including fast modular arithmetic). If you want to do even that by hand, the above FAQ lists several primality test algorithms that you can use as starting points.

blokhead

Replies are listed 'Best First'.
Re^2: Prime Numbers
by KurtSchwind (Chaplain) on Dec 10, 2007 at 01:34 UTC

    I'm really interested in Math::Pari but I haven't been able to get it to install on any of my systems.

    You can easily see the issues that Math::Pari has given people by checking out this pass/fail result test results at cpan. The last time I looked it was 113 passes to 160 failures.

    What really irks me is that Math::Pari seems to be required for Net::SSH::Perl so I haven't been able to get that to work either.

    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
      Hi KurtSchwind,

      I've had good success building Math::Pari in the past on both windows and linux by building against PARI-2.1.4. Version 2.1.7 is probably ok, too - but I think there are issues if you try to build against some of the later versions of PARI (is it the 2.3.x versions ? ... I can't remember). In all cases, I've avoided building PARI first, opting instead to place the PARI source within the Math::Pari source - as outlined in the INSTALL file that ships with Math::Pari. Mind you, I haven't built it for a while, so I'm a little rusty on it.

      As regards SSH, you might like to try Net::SSH2 (for which you'll need libssh2). That's assuming you're interested only in the SSH2 protocol.

      As regards primality testing, Math::GMP (which requires the gmp library) is as good as Math::Pari.

      Cheers,
      Rob

        I tried putting the PARI source in the install directory and it still failed miserably. And it would only bother me in a minor way if it was a single machine, but I can't get this to work on any of my machines.

        I'm not sure if Net::SSH2 will do the task I want. I was hoping Net::SSH::Perl would have better luck. I probably start another thread on what I'm doing specifically and the issues I'm getting. But the jist is that my remote connection does a newgrp which execs another shell (I have no control over this) and it's giving Net::SSH* all sorts of issues.

        --
        I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
      If you are using AS Perl, Randy Kobes has built Math::Pari (v 2.010500) and provides it through PPM.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Nope. I'm running Mac OSX, SuSE, Mandrake and Debian. I wish it were that easy.

        --
        I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.