Skip to content

Latest commit

ย 

History

History
69 lines (48 loc) ยท 1.69 KB

2019-01-30-AT-gcdlcm.md

File metadata and controls

69 lines (48 loc) ยท 1.69 KB

2019๋…„ 1์›” 30์ผ

Lv1 - ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ {docsify-ignore-all}

์ถœ์ฒ˜: https://programmers.co.kr/learn/courses/30/lessons/12940

๋ฌธ์ œ

๋‘ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋‘ ์ˆ˜์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜์™€ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด ๋ณด์„ธ์š”. ๋ฐฐ์—ด์˜ ๋งจ ์•ž์— ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜, ๊ทธ๋‹ค์Œ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋ฅผ ๋„ฃ์–ด ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋‘ ์ˆ˜ 3, 12์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋Š” 3, ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜๋Š” 12์ด๋ฏ€๋กœ solution(3, 12)๋Š” [3, 12]๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ œํ•œ์‚ฌํ•ญ

  • ๋‘ ์ˆ˜๋Š” 1์ด์ƒ 1000000์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

ํ’€์ด

# ๋ฐฉ๋ฒ•1
def gcdlcm(a, b):

    G = 1
    range_num = min(a,b) # ๋‘˜ ์ค‘ ์ž‘์€ ์ˆ˜ ๊นŒ์ง€์˜ ๋ฒ”์œ„๋งŒ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.
    for i in range(range_num, 1, -1):
        if(a%i ==0 and b%i ==0 ):
            G = i
            break

    L = int(a*b/G)
    return [G, L] # [G, L], ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜, ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜

๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

# ๋ฐฉ๋ฒ•2 (์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•)
'''
a๋ฅผ b๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ r์ด๋ผ๊ณ  ํ•˜์ž(a>=b, 0<=r<b)
a์™€b์˜ ์ตœ๋Œ€๊ณต์•ฝ๋ฅผ (a,b)๋ผ๊ณ  ํ•˜๋ฉด ๋‹ค์Œ์ด ์„ฑ๋ฆฝํ•œ๋‹ค.
(a,b) = (b,r)

ex) (1071,1029) = (1029,42) = (42,21) = (21,0) = 21
'''
def gcdlcm(n,m):

    a = max(n,m)
    b = min(n,m)

    remainder = a%b
    while(remainder>0):
        a = b
        b = remainder
        remainder = a%b

    G = b
    L = int(n*m/G)
    return [G,L]

๋ฐฐ์šด์ 

  • a,b์˜ ์ตœ์†Œ๊ณต๋ฐฐ์ˆ˜ = a*b/G (G = a,b์˜ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜)
  • ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•์œผ๋กœ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ๋น ๋ฅด๊ณ  ํšจ์œจ์ ์œผ๋กœ ๊ตฌํ•˜๋Š” ๋ฒ•์„ ์•Œ๊ฒŒ๋˜์—ˆ๋‹ค.

๋Š๋‚€์ 

  • ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๋ฉฐ ์ข€ ๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๊ฐ€๋Š” ์žฌ๋ฏธ๊ฐ€ ์žˆ๋‹ค.