๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป CS

์ตœ์†Œ ๊ณต๋ฐฐ์ˆ˜(์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜) ์ฐพ๊ธฐ - ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ• (๋‚˜๋จธ์ง€ ์ •๋ฆฌ)

by dev.py 2024. 1. 20.

๊ฒฐ๋ก  ๋จผ์ €

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

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

test_number = int(input())
for _ in range(test_number):
    number_1, number_2 = map(int, input().split())
    print(lcm(number_1, number_2))

 

์›๋ฆฌ

์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ• 

a > b ์ผ ๋•Œ, a % b = r ์ด๋ฉด 

a์™€ b์˜ ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜๋Š” b์™€ r์˜ ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜์™€ ๊ฐ™๋‹ค

 

 

๋‚˜๋จธ์ง€ ์ •๋ฆฌ

a % b = r ์ด๋ฉด, 

a = bq + r (q๋Š” ์ž„์˜์˜ ๋ชซ)

๋งŒ์•ฝ d๊ฐ€ a์™€ b์˜ ๊ณต์•ฝ์ˆ˜ ๋ผ๋ฉด, d๋Š” r๋„ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค

์ขŒ๋ณ€

a % d = 0

์šฐ๋ณ€

bq % d = ((b % d ) * (q % d )) % d = 0 (๋‚˜๋จธ์ง€ ์ •๋ฆฌ )

๋”ฐ๋ผ์„œ 0 = 0 + r % d  ์ด๋ฏ€๋กœ  r % d = 0