@@ -1713,7 +1713,7 @@ gmp_gcdext(PyObject *Py_UNUSED(module), PyObject *const *args,
17131713 Py_XDECREF (x );
17141714 Py_XDECREF (y );
17151715 if (ret == ZZ_MEM ) {
1716- PyErr_NoMemory (); /* LCOV_EXCL_LINE */
1716+ return PyErr_NoMemory (); /* LCOV_EXCL_LINE */
17171717 }
17181718 PyObject * tup = PyTuple_Pack (3 , g , s , t );
17191719
@@ -1730,6 +1730,37 @@ gmp_gcdext(PyObject *Py_UNUSED(module), PyObject *const *args,
17301730 return NULL ;
17311731}
17321732
1733+ static PyObject *
1734+ gmp_lcm (PyObject * Py_UNUSED (module ), PyObject * const * args , Py_ssize_t nargs )
1735+ {
1736+ MPZ_Object * res = MPZ_new (0 );
1737+
1738+ if (!res || zz_from_i64 (1 , & res -> z )) {
1739+ return PyErr_NoMemory (); /* LCOV_EXCL_LINE */
1740+ }
1741+ for (Py_ssize_t i = 0 ; i < nargs ; i ++ ) {
1742+ MPZ_Object * arg ;
1743+
1744+ CHECK_OP_INT (arg , args [i ]);
1745+ if (zz_cmp_i32 (& res -> z , 0 ) == ZZ_EQ ) {
1746+ Py_DECREF (arg );
1747+ continue ;
1748+ }
1749+ if (zz_lcm (& res -> z , & arg -> z , & res -> z )) {
1750+ /* LCOV_EXCL_START */
1751+ Py_DECREF (res );
1752+ Py_DECREF (arg );
1753+ return PyErr_NoMemory ();
1754+ /* LCOV_EXCL_STOP */
1755+ }
1756+ Py_DECREF (arg );
1757+ }
1758+ return (PyObject * )res ;
1759+ end :
1760+ Py_DECREF (res );
1761+ return NULL ;
1762+ }
1763+
17331764static PyObject *
17341765gmp_isqrt (PyObject * Py_UNUSED (module ), PyObject * arg )
17351766{
@@ -2071,6 +2102,9 @@ static PyMethodDef gmp_functions[] = {
20712102 {"gcdext" , (PyCFunction )gmp_gcdext , METH_FASTCALL ,
20722103 ("gcdext($module, x, y, /)\n--\n\n"
20732104 "Compute extended GCD." )},
2105+ {"lcm" , (PyCFunction )gmp_lcm , METH_FASTCALL ,
2106+ ("lcm($module, /, *integers)\n--\n\n"
2107+ "Least Common Multiple." )},
20742108 {"isqrt" , gmp_isqrt , METH_O ,
20752109 ("isqrt($module, n, /)\n--\n\n"
20762110 "Return the integer part of the square root of n." )},
@@ -2182,7 +2216,7 @@ gmp_exec(PyObject *m)
21822216 "numbers.Integral.register(gmp.mpz)\n"
21832217 "gmp.fac = gmp.factorial\n"
21842218 "gmp.__all__ = ['comb', 'factorial', 'gcd', 'isqrt',\n"
2185- " 'mpz']\n"
2219+ " 'lcm', ' mpz']\n"
21862220 "gmp.__version__ = imp.version('python-gmp')\n" );
21872221
21882222 PyObject * res = PyRun_String (str , Py_file_input , ns , ns );
0 commit comments