How to factor using Python?

Joe Riopel goon12 at gmail.com
Tue Mar 11 15:40:36 CET 2008


On Tue, Mar 11, 2008 at 10:38 AM, Joe Riopel <goon12 at gmail.com> wrote:
> On Mon, Mar 10, 2008 at 1:08 AM, Nathan Pinno <MadComputerGuy at gmail.com> wrote:
>  > How do I factor a number? I mean how do I translate x! into proper
>  >  Python code, so that it will always do the correct math?
>
>  This should work to do x! (factorial of x).
>
>  reduce(lambda x,y: x*y, range(1, x+1))


Sorry, the variable naming was confusing in that code.
>>> def  dofact(x):
...     return reduce(lambda a,b:  a*b, range(1,x+1))
...
>>> dofact(5)
120
>>>



More information about the Python-list mailing list