Python interview question two (greatest common divisor, least common multiple)

Realize the function of calculating the greatest common divisor and the least common multiple.

def gcd(x, y):
    """求最大公约数"""
    (x, y) = (y, x) if x > y else (x, y)
    for factor in range(x, 0, -1):
        if x % factor == 0 and y % factor == 0:
            return factor
def lcm(x, y):
    """求最小公倍数"""
    return x * y // gcd(x, y)
Published 44 original articles · liked 0 · visits 1226

Guess you like

Origin blog.csdn.net/weixin520520/article/details/105335055