Iteration for Factorials

Nick Craig-Wood nick at craig-wood.com
Wed Oct 24 16:30:16 EDT 2007


Py-Fun <lorna.burns 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.

Here is the math geek answer ;-)

import math

def factorial(i):
    n = i + 1
    return math.exp(-n)*(n**(n-0.5))*math.sqrt(2*math.pi)*(1. + 1./12/n + 1./288/n**2 - 139./51840/n**3)

Works for non integer factorials also...

See here for background

  http://mathworld.wolfram.com/StirlingsSeries.html

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list