Factorials
Tim Rowe
tim at remove_if_not_spam.digitig.cix.co.uk
Sun Jun 8 16:33:12 EDT 2003
On Sat, 07 Jun 2003 15:45:37 +0100, Andrew Wilkinson
<ajw126 at NOSPAMyork.ac.uk> wrote:
>The other replies give a much more readable way of getting the factorial,
>but if you're looking for the ultimate in speed then...
>
>def fac(n):
> return reduce(long.__mul__, range(1,n+1), 1L)
I thought I'd try these out. Taking Anton's challenging test case of
10000:
- The simple recursive solution runs out of stack space.
- Anton's solution barfs on R = [x*y for x,y in zip(R[::2],R[1::2])],
complaining that the sequence index must be an integer.
- Yours works fine.
You're winning!
Is it worth mentioning that for all their elegance recursive solutions
are usually /not/ the way to go unless the language is designed for it
-- especially when the solution is tail-recursive and so can be
replaced with a simple loop anyway...
More information about the Python-list
mailing list