_FAC_TABLE = [1, 1]
def factorial(n):
if n < len(_FAC_TABLE):
return _FAC_TABLE[n]
last = len(_FAC_TABLE) - 1
total = _FAC_TABLE[last]
for i in range(last + 1, n + 1):
total *= i
_FAC_TABLE.append(total)
return total
def main():
from timeit import Timer
print("factorial: %.5f s" %
Timer("factorial(500)", "from __main__ import factorial")
.timeit(1000))
import math
if hasattr(math, "factorial"):
print("math.factorial: %.5f s" %
Timer("factorial(500)", "from math import factorial")
.timeit(1000))
if __name__ == "__main__":
main()
Diff to Previous Revision
--- revision 2 2010-05-23 08:22:50
+++ revision 3 2010-05-23 16:06:33
@@ -5,10 +5,9 @@
last = len(_FAC_TABLE) - 1
total = _FAC_TABLE[last]
- _FAC_TABLE.extend([0] * (n - last))
for i in range(last + 1, n + 1):
total *= i
- _FAC_TABLE[i] = total
+ _FAC_TABLE.append(total)
return total