Solution
class Solution {
static int cnt = 0;
static void countNumOfDiv(int i) {
for (int j = 1; j <= i; j++) {
if (i % j == 0) {
cnt++;
}
}
}
// Even: plus
// Odd: minus
public static int solution(int left, int right) {
int answer = 0;
for (int i = left; i <= right; i++) {
countNumOfDiv(i);
if (cnt % 2 == 0) {
answer += i;
} else { // cnt % 2 == 1
answer -= i;
}
cnt = 0;
}
return answer;
}
public static void main(String[] args) {
System.out.println(solution(13, 17));
System.out.print(solution(24, 27));
}
}
Leave a comment