Calculating factorial

Raymond Hettinger othello at javanet.com
Mon Dec 4 11:53:51 EST 2000


I like your version just fine.  It's simple, clear, and it work's.
Do consider changing line 2 to:  result = 1L so you can
handle fact(50).

If you want to make something hard out of something simple, try:
 def fact(n): return reduce( lambda x,y:x*y, range(2,n+1), 1L )

Raymond

Olli Rajala wrote:

> Hi!
> I've learned a few time Python and programming.  I wrote a function
> that calculates a factorial of N and I was wondering if it's the best
> (I think it's not. =) way to do that. I'm glad to hear your comments.
>
> <script>
>
> def factorial(f):
>     result = 1
>     for number in range(1, f + 1):
>         result = result * number
>     return result
>
> print factorial(10)
>
> </script>
>
> Ps. Sorry my poor English.
> --
> Olli Rajala
> A Finnish student who loves Linux.




More information about the Python-list mailing list