Velocity Reviews > Python bindings for the Gnu Multiprecision library

# Python bindings for the Gnu Multiprecision library

Rick Muller
Guest
Posts: n/a

 01-13-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

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)!

Mensanator
Guest
Posts: n/a

 01-15-2004
>Subject: Python bindings for the Gnu Multiprecision library
>From: http://www.velocityreviews.com/forums/(E-Mail Removed) (Rick Muller)
>Date: 1/13/04 10:44 AM Central Standard Time
>Message-id: <(E-Mail Removed) >
>
>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
>
>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)!

Naturally, Mathworld tells you how to solve these problems using
Mathematica functions. Not having Mathematica, I looked through
GMPY to see if it had something I could use instead and sure
enough, there it was

gmpy.divm(a,b,m): returns x such that b*x == a (mod m)

A single function call saved me from having to iterate through
617 trillion numbers. It's a great package.

--
Mensanator
Ace of Clubs