[Tutor] pickle problems

Dave Angel d at davea.name
Sun Aug 12 16:58:08 CEST 2012


On 08/12/2012 04:44 AM, Richard D. Moores wrote:

> <SNIP>
>
> One caveat is that I have yet to fix the problem with lines 75-78.
>
> One thing I'd like to implement is a monitor of the time
> factorsOfInteger(n) takes to process some of the 18-digit ints (line
> 153). Most are processed within a second or two, but some can take
> several minutes. I'd like to limit the time to 15 or 20 seconds, but
> is there a way to do this? Just a wild guess, but is this where
> threading would be useful?
>
>

Now that you're asking about timing, I have to point out that your
algorithm isn't very fast.  If you could speed it up, perhaps you
wouldn't need to limit the time.

1) You use gmpy2,is_prime().  Seems likely that a library with an
is_prime function might also have some other functions to aid in factoring.

2) You wind up with a floating point number.  If you're getting floats,
then you're limited to their precision, maybe 18 digits, and limited to
their speed.  Perhaps you need to use the // divide rather than the /
one.  And perhaps it'd help to use divmod instead of using % and /
separately.

3) You're doing a % on all the odd numbers above 1, where you really
only need to try the primes.  If you build an iterator for the primes,
you don't need to precalculate them, just cache them for reuse.  Again,
gmp2 is likely to have something useful.

I'm not sure what the point is of preserving the factorizations,
especially since you don't store the number you're factoring for each
case.  Seems to me it'd be more useful to save a list of primes.  But
you've got your reasons i'm sure.

-- 

DaveA



More information about the Tutor mailing list