How to factor using Python?

Lie Lie.1296 at gmail.com
Tue Mar 11 13:06:17 EDT 2008


On Mar 11, 11:47 pm, Lie <Lie.1... at gmail.com> wrote:
> On Mar 10, 12:08 pm, Nathan Pinno <MadComputer... 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?
>
> > Thanks in advance,
> > Nathan P.
>
> Factorial algorithm is a very simple and common algorithm, and it's
> one of the most basic of all recursive algorithm
> def fact(x):
>     return x * fact(x - 1)

As an exercise (actually I forget), I'll let you figure out yourself
why the above function doesn't work. There is only one simple line to
be added and all's done.

Hint: There are two important elements in all recursive algorithm, the
catch-all return statement (which I've given you) and the conditioned
return statement (which you've got to figure out yourself)



More information about the Python-list mailing list