프로그래머스 - 약수의 합

well-life-gm·2021년 11월 6일
0

프로그래머스

목록 보기
35/125

프로그래머스 - 약수의 합

페르마님이 n까지의 약수를 구할 때, n/2까지만 검사해보면 된다 했다.

#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    int answer = n + 1;
    int end = n / 2;
    
    if(n == 0 || n == 1)
        return n;
    
    if(n == 2)
        return 3;
    
    for(int i=2; i<=end; i++) {
        if(n % i)
            continue;
        answer += i;
    }
    return answer;
}
profile
내가 보려고 만든 블로그

0개의 댓글