Safe prime numbers in Python

Skip Montanaro skip at pobox.com
Wed Jan 14 07:16:11 EST 2004


    Tim> Does any know of or have Python code (or C or Fortran code wrapped
    Tim> as a Python module) for efficiently finding safe prime numbers -
    Tim> that is a number p such that p and (p-1)/2 are both prime?

Here's what I came up with using gmpy:

    import gmpy

    def safe_primes(last=1):
        while True:
            next = gmpy.next_prime(last)
            if gmpy.is_prime((next-1)/2):
                yield next
            last = next

Seems to work fairly well.

Skip




More information about the Python-list mailing list