LCM.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:codewars_python 作者: staticor 项目源码 文件源码
def lcm(*args):
    """Compute the least common multiple of some non-negative integers"""
    def lcm2(a, b):
        'compute the least common multiple of a, and b'
        if a == b: return a
        if 0 in (a,b) or 1 in (a,b):return a*b
        m,n = min(a,b), max(a,b)
        while n % m > 0 :
            m, n = divmod(n, m)[1], m
        gcd = m
        return (a*b//gcd)

    if 0 in args: return 0
    args = sorted(args, reverse=True)
    # print(args)
    res = args[0]
    for i in args:
        if res % i >0:
            res = lcm2(res, i)
        else:
            pass
        # print(res)
    return res
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号