How to factor using Python?
Shane Geiger
sgeiger at ncee.net
Tue Mar 11 13:10:19 EDT 2008
def fact(x):
if x == 1: return 1 # don't forget this
else: return x * fact(x - 1)
print fact(5)
> 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)
>
> That recursive function is very close to the basic definition of
> factorials, that is
> x! = x * (x - 1)!
>
> If however, you expected x to be a real (float) value, you'd better
> consider the gamma function as Mark Dickinson pointed out. Wikipedia
> is a good start to create your gamma function:
> http://en.wikipedia.org/wiki/Factorial
> http://en.wikipedia.org/wiki/Gamma_function
>
--
Shane Geiger
IT Director
National Council on Economic Education
sgeiger at ncee.net | 402-438-8958 | http://www.ncee.net
Leading the Campaign for Economic and Financial Literacy
More information about the Python-list
mailing list