[SciPy-user] calculating factorial

Robert Kern robert.kern at gmail.com
Sat Nov 19 08:45:01 CST 2005


Stefan van der Walt wrote:
> I couldn't find "factorial", "combination", "permutation" etc. in
> SciPy.  Luckily, they're simple -- only problem is that my
> implementation doesn't work :)
> 
> See the code listing below.  I thought that the two methods for
> calculating factorials should work equally well.  I get an integer
> overflow, no matter what typecode I use.

100! simply isn't representable by any of the numerical datatypes in
Numeric or scipy_core. It's too large. However, you can use arrays of
Python objects to utilize Python's long integers.

In scipy_core:

In [11]: a  = array(range(2,101), dtype=object)

In [12]: multiply.reduce(a)
Out[12]:
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L

-- 
Robert Kern
robert.kern at gmail.com

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter



More information about the SciPy-user mailing list