BOJ 2609번: 최대공약수와 최대공배수 [Python]

hysong·2022년 6월 23일
0

PS

목록 보기
6/15

📌 Problem.

https://www.acmicpc.net/problem/2609

입력된 두 수의 최대공약수와 최소공배수를 출력하는 문제이다.

📌 Solution.

최대공약수 : GCD(Greatest Common Divisor)
최소공배수 : LCM(Least Common Multiple)
에 대해,

GCD(a, b) = GCD(b, a % b),   if a % b is not zero.
LCM(a, b) = (a * b) / GCD(a, b)

가 성립함을 이용하였다.

import sys

def gcd(a: int, b: int) -> int:
    while (r := a % b) != 0:
        a = b
        b = r
    return b


def lcm(a: int, b: int) -> int:
    return (a * b) // gcd(a, b)


input = sys.stdin.readline
a, b = map(int, input().split())
print(gcd(a, b))
print(lcm(a, b))

0개의 댓글