News for hackers. Stuff that matters.
Re:The Evolution of a Python Programmer by tzs on Thursday March 08, 2012 @02:38PM (#3681211)
If you can live with floating point results:

   from math import gamma
   def factorial(x):
      return gamma(x+1)
This has the advantage of working correctly for non-integer arguments.
Re:The Evolution of a Python Programmer by tomku on Thursday March 08, 2012 @02:55PM (#3681293)
The factorial itself is only defined for non-negative integers. This may seem pedantic, but what you've defined there is actually Gauss's pi function, which has many interesting mathematical properties, but is not equivalent to the factorial because it has a larger domain. The results that it gives for non-integer arguments aren't any more "correct" than a factorial function throwing an exception, because they're both behaving appropriately for their defined domain.

Edit: The math module has a factorial() function anyways, so it's a bit of a moot point. Use that :)

Edit Edit: Thanks for the catch, I did mean non-negative integers instead of positive.

Re:The Evolution of a Python Programmer by JeanPierre on Thursday March 08, 2012 @02:59PM (#3681304)
As far as I know, 0! = 1, so I assume you meant non-negative integers instead of positive integers?
Re:The Evolution of a Python Programmer by JoachimSchipper on Thursday March 08, 2012 @02:48PM (#3681258)
True, but it requires 3.2+ and does not work correctly for large integers (floating-point runs out of precision long before Python's arbitrary-size integers become unusably large.)

(Of course, this is a silly problem.)

Re:The Evolution of a Python Programmer by tzs on Thursday March 08, 2012 @03:37PM (#3681486)
The documentation says it requires 3.2+, but it is working in 2.7 on my Mac, both the 2.7 shipped by Apple and the 2.7 installed by MacPorts.
Re:The Evolution of a Python Programmer by chewxy on Thursday March 08, 2012 @04:00PM (#3681610)
Um, the math module already has a factorial function which performs faster than the gamma function. Why use gamma?
Re:The Evolution of a Python Programmer by tzs on Thursday March 08, 2012 @04:37PM (#3681779)
I didn't know about the factorial function.