2022-01-05


📌 約数と加算(プログラマ1級)
ループ約数の個数と加算(プログラマ1級)
百草
class Solution {
    public int solution(int left, int right) {
        
        int answer = 0;
        
        int[] count = new int[right+1];
        
        for(int i=1; i<=right; i++)
            for(int k=1; k<=right/i; k++)
                count[i*k]++;
        
        for(int i=left; i<=right; i++) {
            if(count[i]%2 ==0){
                answer += i;
                continue;
            }
            
            answer -= i;
    
        }

        return answer;
    }
}
見習う
class Solution {
    public int solution(int left, int right) {
        
        int answer = 0;
        
        for(int i=left; i<=right; i++)
            answer += (i % Math.sqrt(i) == 0 ? -i : i); 

        return answer;
    }
}