0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

最小公倍数 、 最大公約数を求める(Python)

Last updated at Posted at 2022-08-03

今回はhmathで使用されているgcd(list), lcm(list)の計算方法をご紹介します。
gcdは最小公倍数を表し、
lcmは最大公約数を表します。

プログラム

hmath内では次のプログラムで計算しています。

gcd_lcm.py
def gcd(*args):
    if len(args) <= 1:
        return args[0]
    n, k, *others = args
    if k > n:
        n, k = k, n
    while k != 0:
        k, n = n % k, k
    return gcd(n, *others)

def lcm(*args):
    args_max = max(args)
    n = 1
    while True:
        for k in args:
            if args_max * n % k != 0:
                n += 1
                break
        else:
            return args_max * n

最後まで見ていだきありがとうございました。
ぜひ活用してみてください。

0
1
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?