Does a library for prime-related functions exist for Python?

I do not think that there exists such a module dedicated to prime functions in the standard library, but of course there are plenty of people who have written primality tests and such.

One library that is geared towards multiple-precision arithmetic, but which has several functions for primes (such as is_prime() and next_prime()) is GMPY2. The documentation is also available.


I just discovered isprime from the SymPy package:

import sympy
print sympy.isprime(10)

Output:

False

Not to confuse with prime, which returns the n-th prime number:

import sympy
print sympy.prime(10)

Output:

29

gmpy2 supports a variety of pseudoprime tests. The Miller-Rabin test is available as gmpy2.is_strong_prp().

gmpy2 does not have any factorization code yet.

Disclaimer: I'm the maintainer of gmpy2. The primality tests are based on code from http://sourceforge.net/projects/mpzprp/files/

Tags:

Python

Primes