Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Prime Numbers

by blokhead (Monsignor)
on Dec 10, 2007 at 00:24 UTC ( #656028=note: print w/replies, xml ) Need Help??

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.


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.


        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.


      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://656028]
[jdporter]: I think the test for whether to push it to should be strict: (a) you're looking for employment, or (b) you're looking to hire an employee or contractor.

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2017-03-23 01:11 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (278 votes). Check out past polls.