๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
3๏ธโƒฃ Swift/Problem Solving

[Swift Algorithm] ์•ฝ์ˆ˜์˜ ํ•ฉ Programmers(Lv.1)

by seolhee2750 2021. 6. 14.
๋ฌธ์ œ ์„ค๋ช…

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

 

์ œํ•œ ์กฐ๊ฑด
  • n์€ 0 ์ด์ƒ 3000์ดํ•˜์ธ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ
n return
12 28
5 6

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
func solution(_ n:Int) -> Int {
    var sum = 0
    
    for i in 1..<n+1 {
        if n % i == 0 {
            sum += i
        }
    }
    
    return sum
}
  • 1๋ถ€ํ„ฐ ์ฃผ์–ด์ง„ ์ˆ˜ n๊นŒ์ง€ ์ฐจ๋ก€๋กœ ๋‚˜๋ˆ ๋ณด๋ฉฐ ๋‚˜๋จธ์ง€๊ฐ€ 0์ด ๋  ๋•Œ ํ•ด๋‹น ์ˆ˜๋ฅผ ๋”ํ•ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์‰ฌ์šด ๋ฌธ์ œ๋ผ ๊ธˆ๋ฐฉ ํ’€์—ˆ๋‹ค.

 


 

๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/12928

๋Œ“๊ธ€