백준 2869[Python]

김성현·2023년 12월 29일

수학공부

목록 보기
2/3

유클리드 호제법을 사용하여 최소공배수와 최대공약수를 구했다.

유클리드 호제법이 뭐냐하면

When I first learned this method, I was curious why is this possible.
But think about it.
The given number is M and N.
We first divide M by N.

M=NQ1+R1M = N*Q1 + R1

If the remainder is not 0, we test whether R1 is GCD.

N=R1Q2+R2N = R1*Q2 + R2

If R1 is 0, then

N=R1Q2N = R1*Q2
M=R1Q2+R1M = R1*Q2 + R1
M=R1(Q2+1)M = R1(Q2 + 1)

다음과 같이 R1이 최대공약수가 된다.
결국 호제법은 나누었을 때, 나머지가 최대공배수가 될 수 있나를 보는 것이라고 할 수 있다.

코드로 표현하면 다음과 같다.

M,N = map(int,input().split())
mm,nn = M,N
while(M%N!=0):
  M,N = N,M%N
max = N
min = mm*nn//max
print(max)
print(min)
profile
안녕하세요

0개의 댓글