Factorials

Andrew Wilkinson ajw126 at NOSPAMyork.ac.uk
Sat Jun 7 10:45:37 EDT 2003


Rogue9 wrote:

> Pardon me but could anyone enlighten me if python2.2 has a math function
> to call to do factorials of integers?If not then is there a code snippet
> for doing such a thing available?I tried to write some code for this
> myself but with little success.
> Thanks
> Lol

Hi,

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)

..would probably be a bit quicker. I'm not sure how much quicker though.

Anyway, hope this is of some use,
Regards,
Andrew Wilkinson




More information about the Python-list mailing list