Python bindings for the Gnu Multiprecision library

Rick Muller rick_muller at yahoo.com
Tue Jan 13 17:44:41 CET 2004


I just discovered the python bindings for the Gnu Multiprecision
library available at http://gmpy.sf.net. The release notes say that it
only works for Python 2.3, but I successfully got it to work on Python
2.2. Way, way cool.


For example, the following is an implementation of the Lucas-Lehmer
test for Mersenne primes:

def lucas(p):
    "Test whether 2^p-1 is a Mersenne prime"
    s = 4
    val = pow(2,p)-1
    for i in range(3,p+1): s = (s*s-2)%val
    return not s 

Using normal python long integers, this routine is an interesting toy,
but not much else. However, with the gmpy libraries, you can grind
away until your Powerbook G4 burns your lap:

def lucas_gmp(p):
    "Test whether 2^p-1 is a Mersenne prime"
    from gmpy import mpz
    s = mpz('4')
    val = pow(2,p)-1
    for i in range(3,p+1): s = (s*s-2)%val
    return not s 

Way to go, Alex and Gustavo (and anyone else involved)!



More information about the Python-list mailing list