Python bindings for the Gnu Multiprecision library

Discussion in 'Python' started by Rick Muller, Jan 13, 2004.

  1. Rick Muller

    Rick Muller Guest

    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)!
     
    Rick Muller, Jan 13, 2004
    #1
    1. Advertisements

  2. Rick Muller

    Mensanator Guest

    >Subject: Python bindings for the Gnu Multiprecision library
    >From: (Rick Muller)
    >Date: 1/13/04 10:44 AM Central Standard Time
    >Message-id: <>
    >
    >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.

    --
    Mensanator
    Ace of Clubs
     
    Mensanator, Jan 15, 2004
    #2
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter

    1 day gnu, whole life gnu?

    Peter, Jan 10, 2005, in forum: Java
    Replies:
    3
    Views:
    491
    John C. Bollinger
    Jan 10, 2005
  2. Peter
    Replies:
    17
    Views:
    864
    Chris Smith
    Jan 13, 2005
  3. Markus Elfring
    Replies:
    2
    Views:
    545
    Markus Elfring
    Feb 23, 2005
  4. Heiko Wundram
    Replies:
    1
    Views:
    503
    Paul Rubin
    Jun 3, 2004
  5. Martin Maney

    seeking ncpfs library bindings for Python

    Martin Maney, Nov 9, 2005, in forum: Python
    Replies:
    0
    Views:
    339
    Martin Maney
    Nov 9, 2005
  6. Roman Yakovenko
    Replies:
    0
    Views:
    376
    Roman Yakovenko
    Nov 30, 2005
  7. smnoff

    opensolaris C library vs. GNU C Library

    smnoff, Jun 8, 2006, in forum: C Programming
    Replies:
    6
    Views:
    579
  8. Michael Press
    Replies:
    6
    Views:
    495
    M.-A. Lemburg
    Jun 27, 2008
Loading...