Author: Armin Rigo <[email protected]>
Branch:
Changeset: r68393:73fd0ad3e2e2
Date: 2013-12-07 10:22 +0100
http://bitbucket.org/pypy/pypy/changeset/73fd0ad3e2e2/
Log: Issue1654: Improvement to math.factorial() mostly by "anon"
diff --git a/pypy/module/math/app_math.py b/pypy/module/math/app_math.py
--- a/pypy/module/math/app_math.py
+++ b/pypy/module/math/app_math.py
@@ -5,9 +5,24 @@
if fl != x:
raise ValueError("float arguments must be integral")
x = fl
- if x < 0:
- raise ValueError("x must be >= 0")
- res = 1
- for i in range(1, x + 1):
- res *= i
- return res
+ if x <= 100:
+ if x < 0:
+ raise ValueError("x must be >= 0")
+ res = 1
+ for i in range(2, x + 1):
+ res *= i
+ return res
+
+ #Experimentally this gap seems good
+ gap = max(100, x>>7)
+ def _fac(low, high):
+ if low+gap >= high:
+ t = 1
+ for i in range(low, high):
+ t *= i
+ return t
+
+ mid = (low + high) >> 1
+ return _fac(low, mid) * _fac(mid, high)
+
+ return _fac(1, x+1)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit