Skip to content

bpo-39479:Add math.lcm() function: Least Common Multiple #18328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 83 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
78c612a
Update 3.9.rst
ananthan-123 Feb 3, 2020
8300b4e
Update math.rst
ananthan-123 Feb 3, 2020
4c215e8
Update test_math.py
ananthan-123 Feb 3, 2020
5ad99af
Update mathmodule.c
ananthan-123 Feb 3, 2020
c373139
Update Doc/library/math.rst
ananthan-123 Feb 3, 2020
a23ba14
Update Doc/library/math.rst
ananthan-123 Feb 3, 2020
b540fc7
Update Lib/test/test_math.py
ananthan-123 Feb 3, 2020
13be573
Update Doc/library/math.rst
ananthan-123 Feb 3, 2020
a3a36ab
Update mathmodule.c
ananthan-123 Feb 3, 2020
34efffa
Update test_math.py
ananthan-123 Feb 3, 2020
e42b00c
Update mathmodule.c
ananthan-123 Feb 3, 2020
091c4a6
Update mathmodule.c
ananthan-123 Feb 3, 2020
99b1364
Update mathmodule.c
ananthan-123 Feb 3, 2020
a5d89b5
Update math.rst
ananthan-123 Feb 3, 2020
7823c12
Update math.rst
ananthan-123 Feb 3, 2020
ce474a8
Update Doc/library/math.rst
ananthan-123 Feb 3, 2020
aa9abb7
Update Doc/library/math.rst
ananthan-123 Feb 3, 2020
79bf932
Update math.rst
ananthan-123 Feb 3, 2020
972f8e6
Update math.rst
ananthan-123 Feb 4, 2020
7598895
📜🤖 Added by blurb_it.
blurb-it[bot] Feb 4, 2020
42669da
Update math.rst
ananthan-123 Feb 4, 2020
4ee5b20
Update mathmodule.c
ananthan-123 Feb 4, 2020
97a54db
Update mathmodule.c
ananthan-123 Feb 4, 2020
4072d92
Update math.rst
ananthan-123 Feb 4, 2020
38fa5ed
Update math.rst
ananthan-123 Feb 4, 2020
e616c74
Update test_math.py
ananthan-123 Feb 4, 2020
fbb064a
Update mathmodule.c
ananthan-123 Feb 4, 2020
a72f5dd
Update mathmodule.c
ananthan-123 Feb 4, 2020
049d867
Update Lib/test/test_math.py
ananthan-123 Feb 4, 2020
6ed8074
Update test_math.py
ananthan-123 Feb 4, 2020
e18e43b
Update mathmodule.c
ananthan-123 Feb 4, 2020
4e3c1cd
Update test_math.py
ananthan-123 Feb 4, 2020
d8b03f9
Update mathmodule.c
ananthan-123 Feb 4, 2020
389c497
Update mathmodule.c.h
ananthan-123 Feb 4, 2020
97da5b8
Update mathmodule.c
ananthan-123 Feb 4, 2020
bc6490b
Update mathmodule.c
ananthan-123 Feb 5, 2020
227dee6
Update mathmodule.c
ananthan-123 Feb 6, 2020
177d42c
Update Lib/test/test_math.py
ananthan-123 Feb 7, 2020
e7e5a84
Update Lib/test/test_math.py
ananthan-123 Feb 7, 2020
26ca487
Update mathmodule.c
ananthan-123 Feb 7, 2020
30f0226
Update mathmodule.c
ananthan-123 Feb 7, 2020
08b1fd2
Update mathmodule.c
ananthan-123 Feb 7, 2020
d504b86
Update mathmodule.c
ananthan-123 Feb 7, 2020
53d3af1
Update mathmodule.c
ananthan-123 Feb 7, 2020
0700ed6
Update mathmodule.c
ananthan-123 Feb 7, 2020
e458507
Update mathmodule.c
ananthan-123 Feb 7, 2020
67aad61
Update math.rst
ananthan-123 Feb 7, 2020
517912b
Update 3.9.rst
ananthan-123 Feb 7, 2020
375c258
Update math.rst
ananthan-123 Feb 7, 2020
76fed5d
Update mathmodule.c
ananthan-123 Feb 7, 2020
d128e2d
Update test_math.py
ananthan-123 Feb 7, 2020
e009376
Update test_math.py
ananthan-123 Feb 7, 2020
4a4f241
Update mathmodule.c
ananthan-123 Feb 7, 2020
a596a83
Update mathmodule.c
ananthan-123 Feb 7, 2020
92cd405
Update test_math.py
ananthan-123 Feb 7, 2020
c069214
Update mathmodule.c
ananthan-123 Feb 7, 2020
8543b03
Update mathmodule.c
ananthan-123 Feb 7, 2020
4566c08
Update mathmodule.c
ananthan-123 Feb 7, 2020
66e6d65
Update mathmodule.c
ananthan-123 Feb 7, 2020
c063ea3
Update test_math.py
ananthan-123 Feb 7, 2020
f457948
Update test_math.py
ananthan-123 Feb 7, 2020
ea72735
Update mathmodule.c
ananthan-123 Feb 7, 2020
46f10f2
Update mathmodule.c
ananthan-123 Feb 7, 2020
5b4428b
Update mathmodule.c
ananthan-123 Feb 7, 2020
6caf900
Update mathmodule.c
ananthan-123 Feb 8, 2020
911570b
Update test_math.py
ananthan-123 Feb 8, 2020
24ba4c8
Update mathmodule.c
ananthan-123 Feb 8, 2020
b097bc6
Update test_math.py
ananthan-123 Feb 8, 2020
ddf5a39
Update mathmodule.c
ananthan-123 Feb 15, 2020
37f5233
Update mathmodule.c
ananthan-123 Feb 15, 2020
3074e34
Update test_math.py
ananthan-123 Feb 15, 2020
3bb50dc
Update mathmodule.c
ananthan-123 Feb 15, 2020
e40c94c
Update mathmodule.c
ananthan-123 Feb 15, 2020
2e95203
Update mathmodule.c
ananthan-123 Feb 15, 2020
afdecfa
Update mathmodule.c
ananthan-123 Feb 15, 2020
aa118d9
changed '&&' to '||'
ananthan-123 Feb 15, 2020
0c03453
changed gramatical errors
ananthan-123 Feb 15, 2020
73fe2af
Update math.rst
ananthan-123 Feb 15, 2020
20e696f
removed "else" statement.
ananthan-123 Feb 15, 2020
54d1be4
Update Misc/NEWS.d/next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst
ananthan-123 Feb 15, 2020
f4ec251
Update test_math.py
ananthan-123 Feb 15, 2020
134c4f4
Update math.rst
ananthan-123 Feb 16, 2020
2db3ca7
Changed nonzero to both
ananthan-123 Feb 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Doc/library/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ Number-theoretic and representation functions
.. versionadded:: 3.5


.. function:: lcm(a, b)

Return the least common multiple of integers *a* and *b*. The value of
``lcm(a, b)`` is the smallest nonnegative integer that is a multiple of
both *a* and *b*. If either *a* or *b* is zero then ``lcm(a, b)`` is zero.

.. versionadded:: 3.9


.. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)

Return ``True`` if the values *a* and *b* are close to each other and
Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ Add :func:`math.ulp`: return the value of the least significant bit
of a float.
(Contributed by Victor Stinner in :issue:`39310`.)

Add :func:`math.lcm`: return the least common multiple of *a* and *b*.
(Contributed by Ananthakrishnan in :issue:`39479`.)

nntplib
-------

Expand Down
35 changes: 35 additions & 0 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,41 @@ def __index__(self):
with self.assertRaises(TypeError):
math.isqrt(value)

def test_lcm(self):
lcm = math.lcm
self.assertEqual(lcm(0, 0), 0)
self.assertEqual(lcm(1, 0), 0)
self.assertEqual(lcm(-1, 0), 0)
self.assertEqual(lcm(0, 1), 0)
self.assertEqual(lcm(0, -1), 0)
self.assertEqual(lcm(7, 1), 7)
self.assertEqual(lcm(7, -1), 7)
self.assertEqual(lcm(-23, 15), 345)
self.assertEqual(lcm(120, 84), 840)
self.assertEqual(lcm(84, -120), 840)
self.assertEqual(lcm(1216342683557601535506311712,
436522681849110124616458784),
16592536571065866494401400422922201534178938447014944)
x = 434610456570399902378880679233098819019853229470286994367836600566
y = 1064502245825115327754847244914921553977

for c in (652560,
576559230871654959816130551884856912003141446781646602790216406874):
a = x * c
b = y * c
d = x * y * c
self.assertEqual(lcm(a, b), d)
self.assertEqual(lcm(b, a), d)
self.assertEqual(lcm(-a, b), d)
self.assertEqual(lcm(b, -a), d)
self.assertEqual(lcm(a, -b), d)
self.assertEqual(lcm(-b, a), d)
self.assertEqual(lcm(-a, -b), d)
self.assertEqual(lcm(-b, -a), d)
self.assertEqual(lcm(MyIndexable(120), MyIndexable(84)), 840)
self.assertRaises(TypeError, lcm, 120.0, 84)
self.assertRaises(TypeError, lcm, 120, 84.0)

def testLdexp(self):
self.assertRaises(TypeError, math.ldexp)
self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add func:`math.lcm` function: Least Common Multiple.
32 changes: 31 additions & 1 deletion Modules/clinic/mathmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2016,6 +2016,60 @@ math_factorial(PyObject *module, PyObject *arg)
}


/*[clinic input]
math.lcm

x as a: object
y as b: object
/
least common multiple of x and y
[clinic start generated code]*/

static PyObject *
math_lcm_impl(PyObject *module, PyObject *a, PyObject *b)
/*[clinic end generated code: output=6f83fb6d671074ba input=bd41b785dc2a4ff1]*/
{
PyObject *g, *m, *f, *ab;

a = PyNumber_Index(a);
if (a == NULL) {
return NULL;
}
b = PyNumber_Index(b);
if (b == NULL) {
Py_DECREF(a);
return NULL;
}
if (_PyLong_Sign(a) == 0 || _PyLong_Sign(b) == 0) {
Py_DECREF(a);
Py_DECREF(b);
return PyLong_FromLong(0);
}
g = _PyLong_GCD(a, b);
if (g == NULL) {
Py_DECREF(a);
Py_DECREF(b);
return NULL;
}
f = PyNumber_FloorDivide(a, g);
Py_DECREF(g);
Py_DECREF(a);
if (f == NULL) {
Py_DECREF(b);
return NULL;
}
m = PyNumber_Multiply(f, b);
Py_DECREF(f);
Py_DECREF(b);
if (m == NULL) {
return NULL;
}
ab = PyNumber_Absolute(m);
Py_DECREF(m);
return ab;
}


/*[clinic input]
math.trunc

Expand Down Expand Up @@ -3361,6 +3415,7 @@ static PyMethodDef math_methods[] = {
MATH_ISINF_METHODDEF
MATH_ISNAN_METHODDEF
MATH_ISQRT_METHODDEF
MATH_LCM_METHODDEF
MATH_LDEXP_METHODDEF
{"lgamma", math_lgamma, METH_O, math_lgamma_doc},
MATH_LOG_METHODDEF
Expand Down