least common multiple Algorithm

The least common multiple (LCM) algorithm is a mathematical method used to find the smallest multiple that is evenly divisible by two or more given numbers. This concept is widely used in number theory, elementary arithmetic, and various fields of mathematics to solve problems related to divisibility and fractions. LCM has significant applications in real-life situations as well, such as scheduling events or finding a common period for repeating patterns. The LCM algorithm can be determined using various techniques, including the prime factorization method, division method, and listing multiples method. One popular way to find the LCM of two numbers is by using their prime factorization. In this method, both numbers are broken down into their prime factors, then the LCM is calculated by multiplying the highest power of all the prime factors present in either of the two given numbers. For instance, to find the LCM of 12 and 15, the prime factorization for 12 is 2^2 * 3^1, and for 15 is 3^1 * 5^1. The LCM would then be calculated by taking the highest power of each prime factor: 2^2 * 3^1 * 5^1, which equals 60. This method is particularly efficient when dealing with larger numbers or when using a computer algorithm to calculate the LCM. Other methods, such as the division method or listing multiples, can also be used to find the LCM, but they may be more time-consuming or cumbersome when dealing with larger numbers.
import unittest


def find_lcm(first_num: int, second_num: int) -> int:
    """Find the least common multiple of two numbers.

       Learn more: https://en.wikipedia.org/wiki/Least_common_multiple

       >>> find_lcm(5,2)
       10
       >>> find_lcm(12,76)
       228
    """
    max_num = first_num if first_num >= second_num else second_num
    common_mult = max_num
    while (common_mult % first_num > 0) or (common_mult % second_num > 0):
        common_mult += max_num
    return common_mult


class TestLeastCommonMultiple(unittest.TestCase):

    test_inputs = [
        (10, 20),
        (13, 15),
        (4, 31),
        (10, 42),
        (43, 34),
        (5, 12),
        (12, 25),
        (10, 25),
        (6, 9),
    ]
    expected_results = [20, 195, 124, 210, 1462, 60, 300, 50, 18]

    def test_lcm_function(self):
        for i, (first_num, second_num) in enumerate(self.test_inputs):
            actual_result = find_lcm(first_num, second_num)
            with self.subTest(i=i):
                self.assertEqual(actual_result, self.expected_results[i])


if __name__ == "__main__":
    unittest.main()

LANGUAGE:

DARK MODE: