-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
💬 문제
[코딩테스트 연습 - 약수의 합](https://school.programmers.co.kr/learn/courses/30/lessons/12928)
💬 풀이
import Foundation
func solution(_ n:Int) -> Int {
var result = 0
if n == 0 {
return 0
}
for i in 1...Int(sqrt(Double(n))) {
if n % i == 0 {
if i != n / i {
result += (n / i)
}
result += i
}
}
return result
}
💬 더 나은 방법?
func solution(_ n:Int) -> Int {
guard n != 0 else { return 0 }
return Array(1...n).filter{ n % $0 == 0 }.reduce(0, +)
}
💬 알게된 것
- 제곱근을 주의할것!!