Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] 약수의 합 #61

Closed
hwangJi-dev opened this issue Jan 10, 2023 · 0 comments
Closed

[Algorithm] 약수의 합 #61

hwangJi-dev opened this issue Jan 10, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Jan 10, 2023

💬 문제

[코딩테스트 연습 - 약수의 합](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, +)
}

💬 알게된 것

  • 제곱근을 주의할것!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant