Pythonで最大公約数と最小公倍数
最大公約数(gcd)
2つの値の最大公約数
import math a, b = map(int, imput().split()) gcd = math.gcd(a,b)
複数の値の最大公約数
import math n = list(map(int input().split())) gcd = n[0] for i in range(2,n+1): gcd = math.gcd(gcd,i)
最小公倍数(lcm)
2つの値の最小公倍数
import math a, b = map(int, imput().split()) lcm = a*b // math.gcd(a,b)
複数の値の最小公倍数
import math def LCM(a, b): return a*b // math.gcd(a,b) n = list(map(int input().split())) lcm = 1 for i in range(2,n+1): lcm = LCM(lcm, i)