Skip to content

Commit 3cc3531

Browse files
yuriimchgcclauss
authored andcommitted
Feature/update least common multiple (TheAlgorithms#1352)
* renamed module to extend the acronym * add type hints (will not work with Python less than 3.4) * update docstring * refactor the function * add unittests for the least common squares multiple
1 parent 870eebf commit 3cc3531

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

maths/find_lcm.py

-34
This file was deleted.

maths/least_common_multiple.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import unittest
2+
3+
4+
def find_lcm(first_num: int, second_num: int) -> int:
5+
"""Find the least common multiple of two numbers.
6+
7+
Learn more: https://en.wikipedia.org/wiki/Least_common_multiple
8+
9+
>>> find_lcm(5,2)
10+
10
11+
>>> find_lcm(12,76)
12+
228
13+
"""
14+
max_num = first_num if first_num >= second_num else second_num
15+
common_mult = max_num
16+
while (common_mult % first_num > 0) or (common_mult % second_num > 0):
17+
common_mult += max_num
18+
return common_mult
19+
20+
21+
class TestLeastCommonMultiple(unittest.TestCase):
22+
23+
test_inputs = [
24+
(10, 20),
25+
(13, 15),
26+
(4, 31),
27+
(10, 42),
28+
(43, 34),
29+
(5, 12),
30+
(12, 25),
31+
(10, 25),
32+
(6, 9),
33+
]
34+
expected_results = [20, 195, 124, 210, 1462, 60, 300, 50, 18]
35+
36+
def test_lcm_function(self):
37+
for i, (first_num, second_num) in enumerate(self.test_inputs):
38+
actual_result = find_lcm(first_num, second_num)
39+
with self.subTest(i=i):
40+
self.assertEqual(actual_result, self.expected_results[i])
41+
42+
43+
if __name__ == "__main__":
44+
unittest.main()

0 commit comments

Comments
 (0)