[Tutor] pickle problems

eryksun eryksun at gmail.com
Sun Aug 12 19:49:19 CEST 2012


On Sun, Aug 12, 2012 at 10:58 AM, Dave Angel <d at davea.name> wrote:
>
> 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.

Good catch in Kent Johnson's code. Maybe he'll search for his name and
find this. It should be `r = r // factor`.

> 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.

@Richard, if you haven't learned generators, see the docs for yield
expressions:

http://docs.python.org/py3k/reference/expressions.html#yield-expressions
http://www.python.org/dev/peps/pep-0255

First yield the cached primes. Then compute the next prime, cache it,
and yield it. Use the cache to speed up finding the next prime. You
can pickle the cache of primes instead of the factorizations.

> 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.

The numbers are the dictionary keys.


More information about the Tutor mailing list