알고리즘/수학문제

[프로그래머스] 약수의 개수와 덧셈 #Java

VIPeveloper 2022. 5. 20. 15:41
728x90
반응형
import java.io.IOException;
import java.util.*;

class Main {
    public static void main(String[] args) throws IOException {
        int l = 13;
        int r = 17;

        System.out.println(solution(l,r));
    }
    public static int solution(int left, int right) {
        int answer = 0;
        for (int i = left; i <= right; i++) {
            if(evenOdd(i)){
                answer+=i;
            }else{
                answer-=i;
            }
        }
        return answer;
    }

    private static boolean evenOdd(int i) {
        int cnt = 0;
        for (int j = 1; j <= i; j++) {
            if(i%j==0){
                cnt++;
            }
        }
        return cnt%2==0;
    }
}
728x90
반응형