Skip to content

Latest commit

ย 

History

History
49 lines (39 loc) ยท 1.34 KB

2019-02-14-AT-sum_divisor.md

File metadata and controls

49 lines (39 loc) ยท 1.34 KB

2019๋…„ 2์›” 14์ผ

Lv1 - ์•ฝ์ˆ˜์˜ ํ•ฉ {docsify-ignore-all}

์ถœ์ฒ˜: https://programmers.co.kr/learn/courses/30/lessons/12928?language=python3#

๋ฌธ์ œ

์ž์—ฐ์ˆ˜ n์„ ์ž…๋ ฅ๋ฐ›์•„ n์˜ ์•ฝ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • n์€ 0 ์ด์ƒ 3000์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

ํ’€์ด

def sum_divisor(n):
    sum = 0
    for i in range(1,n+1):
        if n%i == 0:
           sum += i
    return sum

๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•

import math
def sum_divisor2(n):
    sum = 0
    mid = int(math.sqrt(n))

    for i in range(1, mid+1):
        if n % i == 0:
            if i*i == n: # i์ œ๊ณฑ์ด n์ผ ๊ฒฝ์šฐ๋Š” ํ•œ ๋ฒˆ๋งŒ ๋”ํ•ด์ค˜์•ผ ํ•œ๋‹ค. (ex. 25์˜ ์•ฝ์ˆ˜: 1, 5 ,25)
                sum += i
            else:
                sum += i + n/i
    return int(sum)

๋ฐฐ์šด์ 

  • ์•ฝ์ˆ˜์˜ ํŠน์„ฑ: n=a*b (๋‹จ, a <= b)
  • ์•ฝ์ˆ˜ a,b๋Š” ์Œ์œผ๋กœ ์กด์žฌํ•œ๋‹ค.
  • a,b๋Š” ๋ฐ˜๋น„๋ก€ ๊ด€๊ณ„์ด๋‹ค.
  • a์˜ ์ตœ๋Œ€๊ฐ’์€ a == b ์ผ ๋•Œ ์ด๋‹ค.
  • ๋”ฐ๋ผ์„œ ์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•  ๋•Œ a == b ์ผ๋•Œ ๊นŒ์ง€๋งŒ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.(์ฆ‰, n = a*a ๊ฐ€ ๋˜๋Š” ์‹œ์ )

๋Š๋‚€์ 

  • ์‰ฌ์šด ํ’€์ด์™€ ํšจ์œจ์ ์ธ ํ’€์ด๊ฐ€ ์žˆ๋‹ค. ๋ณดํ†ต ์‰ฌ์šด ํ’€์ด๊ฐ€ ๋จผ์ € ๋– ์˜ฌ๋ผ ๊ทธ๋ ‡๊ฒŒ ํ’€๊ฒŒ ๋œ๋‹ค.
  • ํ’€์—ˆ๋‹ค๊ณ  ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ€์ง€ ๋ง๊ณ  ์ข€ ๋” ํšจ์œจ์ ์ธ ํ’€์ด๊ฐ€ ์žˆ๋Š”์ง€ ๊ณ ๋ฏผํ•˜์ž.