技術メモ

役に立てる技術的な何か、時々自分用の覚書。幅広く色々なことに興味があります。

GCD, LCM with Python3

自分用のメモ
Python3で最小公倍数(GCD)、最大公約数(LCM)を求める

from functools import reduce

def gcd(*numbers):
    def gcd(a, b):
        while b != 0:
            a, b = b, a % b
        return a
    return reduce(gcd, numbers)

def lcm(*numbers):
    def lcm(a, b):
        return (a * b) // gcd(a, b)
    return reduce(lcm, numbers, 1)