Drkcore

12 01 2010 Python cpp Tweet

SRM162-DIV2-250

最小と最大の数を与えられた時にそのレンジの数の最小公倍数を求める。

順繰りにかけていって、その際に最大公約数で割ってく。最大公約数はユークリッドの互除法で。

def lcm(first,last):
    l =1  
    for i in range(first,last):
        l = l * i / gcd(l,i)
    return l 

def gcd(a,b):
    if(b==0): return a
    return gcd(b, a % b)

print lcm(1,5)
print lcm(4,5)
print lcm(1,12)

実行

/usr/bin/python  /Users/kzfm/python/lcmr.py 
12
4
27720

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021