Iteration for Factorials
mensanator at aol.com
mensanator at aol.com
Mon Oct 22 13:57:25 EDT 2007
More information about the Python-list mailing list
Mon Oct 22 13:57:25 EDT 2007
- Previous message (by thread): Iteration for Factorials
- Next message (by thread): Iteration for Factorials
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Oct 22, 7:50 am, Duncan Booth <duncan.bo... at invalid.invalid> wrote: > Py-Fun <lorna.bu... at gmail.com> wrote: > > I'm stuck trying to write a function that generates a factorial of a > > number using iteration and not recursion. Any simple ideas would be > > appreciated. > > This version avoids doing anything fancier than adding 1, so it should be > simple enough for anyone: > > def factorial(e): > a = 1 > for b in range(e): > c = 0 > for j in range(b, -1, -1): > for d in range(a): > c += 1 > a = c > return a Not simple enough for my taste: >>> import gmpy >>> gmpy.fac(10) mpz(3628800)
- Previous message (by thread): Iteration for Factorials
- Next message (by thread): Iteration for Factorials
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list