Calculating factorial

Daniel Klein DanielK at aracnet.com
Tue Dec 5 04:29:58 CET 2000


Other than the the previous recommendation to use 1L, you could reduce the
number of iterations by 1 by starting at 2 in the 'for' loop, ie

    for number in range(2, f + 1):

No need to multiply 'result' times 1.

Daniel Klein


"Olli Rajala" <ollir at linuxfreak.com> wrote in message
news:3a2a9496.647995 at uutiset.nic.fi...
> 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