X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/25e3ebd45318f009677e79dc8768cfc1b7810f0f..4e5cc42b1384e02fcbdd2c5673f8a33e9fd807b9:/pock diff --git a/pock b/pock index a980cbe..5d7cc50 100644 --- a/pock +++ b/pock @@ -27,7 +27,7 @@ ###-------------------------------------------------------------------------- ### Imported modules. -from sys import argv, stdin, stdout, stderr +import sys as SYS; from sys import argv, stdin, stdout, stderr import os as OS import itertools as I import math as M @@ -35,6 +35,10 @@ import optparse as OP import catacomb as C +if SYS.version_info >= (3,): + xrange = range + range = lambda *args: list(xrange(*args)) + ###-------------------------------------------------------------------------- ### Utilities. @@ -49,12 +53,8 @@ class ExpectedError (Exception): pass def prod(ff, one = 1): - """ - Return ONE times the product of the elements of FF. - - This is not done very efficiently. - """ - return reduce(lambda prod, f: prod*f, ff, one) + """Return ONE times the product of the elements of FF.""" + return C.MPMul().factor(one).factor(ff).done() def parse_label(line): """ @@ -222,8 +222,10 @@ class Sieve (object): ## Figure out the number of bits in a (nonnegative) primitive `int'. We'll ## use a list of these as our sieve. + try: _MAX = SYS.maxint + except AttributeError: _MAX = SYS.maxsize _NBIT = 15 - while type(1 << (_NBIT + 1)) == int: _NBIT += 1 + while 1 << (_NBIT + 1) < _MAX: _NBIT += 1 def __init__(me, limit): """ @@ -240,8 +242,8 @@ class Sieve (object): me.limit = limit ## Calculate the size of sieve we'll need and initialize the bit list. - n = (limit - 2)/2 - sievesz = (n + me._NBIT - 1)/me._NBIT + n = (limit - 2)//2 + sievesz = (n + me._NBIT - 1)//me._NBIT me._sievemax = sievesz*me._NBIT me._bits = sievesz*[0] @@ -283,7 +285,7 @@ def initsieve(sievebits): global SIEVE if SIEVE is not None: raise ValueError('sieve already defined') if sievebits < 6: sievebits = 6 - SIEVE = Sieve(1 << (sievebits + 1)/2) + SIEVE = Sieve(1 << (sievebits + 1)//2) ###-------------------------------------------------------------------------- ### Primality checking. @@ -900,7 +902,7 @@ def gen(nbits, label = None, p = ProgressReporter()): Give it the LABEL, and report progress to P. """ - if SIEVE.limit >> (nbits + 1)/2: g = gen_small + if SIEVE.limit >> (nbits + 1)//2: g = gen_small else: g = gen_pock return g(nbits, label = label, p = p)