Python bindings for the Gnu Multiprecision library

R

Rick Muller

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

Mensanator

Subject: Python bindings for the Gnu Multiprecision library
From: (e-mail address removed) (Rick Muller)
Date: 1/13/04 10:44 AM Central Standard Time
Message-id: <[email protected]>

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


The other day I was reading about Linear Congruence on Mathworld.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top