유클리드 호제법을 활용하면 풀면 된다
숫자 a,b가 있을 때 a를 b로 나눈 나머지와 b의 최대공약수는 a와 b의 최대공약수와 같다.
def gcd(x,y) :
while y > 0:
x, y = y,x%y
return x
최소공배수 X 최대공약수 = a X b
def lcd(x,y) :
return x*y//gcd(x,y)
.
.
근데 사실 파이썬 내에 math.gcd(), math.lcd() 함수를 사용해도 된다..
a,b = map(int, input().split())
def gcd(x,y) :
while y > 0:
x, y = y,x%y
return x
print(gcd(a,b))
def lcd(x,y) :
return x*y//gcd(x,y)
print(lcd(a,b))
위와같이 유클리드 호제법으로 풀었다
import sys
n = int(sys.stdin.readline())
arr = []
def gcd(a,b):
while b>0 :
a , b = b, a%b
return a
def lcd(a,b) :
return a*b//gcd(a,b)
for _ in range(n) :
a,b = map(int, sys.stdin.readline().split())
arr.append(lcd(a,b))
for i in arr :
print(i)