SoFunction
Updated on 2025-03-04

Example of method implementation of minimum common multiple Python

Least common multiple = product of two numbers / greatest common divisor

Transition and phase division (Euclidean law)

The principle of implementing this method is to find the remainder of two positive integers.r, then use the smaller number among the two positive integers to calculate the remainder until the remainder is0When, the smaller number at this time is the greatest common divisor. Finally, the formula is used to calculate the minimum common multiple of these two numbers.

Code example:

print("Please enter two positive integers:")
m = int(input())
n = int(input())
x = m * n  # x is used to store the product of m and nprint(f"{m}and{n}The minimum common multiple is:", end='')  # At this time, the values ​​of m and n output have not changed yetr = m % n
while r != 0:  # No need to compare sizes. If m is smaller than n, it will be swapped at the first cycle    m = n
    n = r
    r = m % n
print(x // n)

Subtraction method (more-phase subtraction method)

This method is easier to understand. The principle is to first judge the size of two positive integers, and assign the difference between the larger number and the smaller number to the larger number. Cycle this step until the two numbers are equal, and then the greatest common divisor is obtained. Finally, the formula is used to calculate the minimum common multiple of these two numbers.

Code example:

print("Please enter two positive integers:")
m = int(input())
n = int(input())
x = m * n  # x is used to store the product of m and nprint(f"{m}and{n}The minimum common multiple is:", end='')  # At this time, the values ​​of m and n output have not changed yetwhile m != n:
    if m > n:
        m = m - n
    else:
        n = n - m
print(x // m)

Summarize

This is the end of this article about the implementation of the minimum common multiple Python. For more relevant content on the implementation of the minimum common multiple Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!