chiark / gitweb /
mp.c: Proper binding for `mp_factorial'.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 27 Feb 2015 14:40:01 +0000 (14:40 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 27 Feb 2015 14:40:01 +0000 (14:40 +0000)
Rather than using the cardboard one in Python.

catacomb/__init__.py
mp.c

index 0f0aa9973028bc86809705b3f0cb421cd49a4927..93f66da2287ecac37e9c1beaa96b53d456c39415 100644 (file)
@@ -108,11 +108,6 @@ class _tmp:
   def mont(x): return MPMont(x)
   def barrett(x): return MPBarrett(x)
   def reduce(x): return MPReduce(x)
-  def factorial(x):
-    'factorial(X) -> X!'
-    if x < 0: raise ValueError, 'factorial argument must be > 0'
-    return MPMul.product(xrange(1, x + 1))
-  factorial = staticmethod(factorial)
 _augment(MP, _tmp)
 
 class _tmp:
diff --git a/mp.c b/mp.c
index 705a65eef18a3a6c60d445019851f95de8c96fb0..038324a8e1f1cc2a97d29a28b4035925004efd34 100644 (file)
--- a/mp.c
+++ b/mp.c
@@ -927,6 +927,16 @@ end:
   return (z);
 }
 
+static PyObject *meth__MP_factorial(PyObject *me, PyObject *arg)
+{
+  unsigned long i;
+  mp *x;
+  if (!PyArg_ParseTuple(arg, "OO&:factorial", &me, convulong, &i))
+    return (0);
+  x = mp_factorial(i);
+  return mp_pywrap(x);
+}
+
 #define LOADOP(pre, py, name)                                          \
   static PyObject *meth__##py##_##name(PyObject *me, PyObject *arg)    \
   {                                                                    \
@@ -2446,6 +2456,8 @@ fromstring(STR, radix = 0) -> (X, REST)\n\
 Parse STR as a binary polynomial, according to radix.  If radix is zero,\n\
 read a prefix from STR to decide radix: allow `0' for octal, `0x' for hex\n\
 or `R_' for other radix R.")
+  METH (_MP_factorial,         "\
+factorial(I) -> I!: compute factorial")
   METH (_MP_loadl,             "\
 loadl(STR) -> X: read little-endian bytes")
   METH (_MP_loadb,             "\