Anagram

Bengt Richter bokr at oz.net
Thu Jan 24 03:35:59 EST 2002


On Wed, 23 Jan 2002 16:56:24 +0100, "Alex Martelli" <aleax at aleax.it> wrote:

><montanaro at tttech.com> wrote in message
>news:mailman.1011796737.3826.python-list at python.org...
>>
>>     Eric> BTW, I thought there was a built-in function returning x!
>>     Eric> somewhere, but I didn't find it... Was it a dream?
>>
>> Yup... ;-)  Factorial is one of those functions that's so easily written
>> that it's not worth stuffing in a standard module somewhere.
>
>...except maybe when you need it to be *REALLY, REALLY FAST*:

[...speed tests...]

Well, that's looking for the fastest way to do the same algorithm (unless
gmpy.fac() does something different) ...

I suspect that for large numbers at some point it would benefit you
to multiply the factors 2..n in small groups, then groups of their products,
etc., trying to group so as to keep products more or less comparable in size.

Perhaps there's also a memoization optimization in there, if the factors
are generated in an interesting way (is there a fast way to generate
the equivalent list of prime factors (with repeats for common factors)
and selecting out identical tuples plus leftovers?) ... but not tonight ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list