240514

๋ฌธ์ œ : ์•ฝ์ˆ˜์˜ ๊ฐœ์™€ ๋ง์…ˆ ๋‚œ์ด๋„ : Lv.1 ์„ค๋ช… :

๋‘ ์ •์ˆ˜ left์™€ right๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. left๋ถ€ํ„ฐ right๊นŒ์ง€์˜ ๋ชจ๋“  ์ˆ˜๋“ค ์ค‘์—์„œ, ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ง์ˆ˜์ธ ์ˆ˜๋Š” ๋”ํ•˜๊ณ , ์•ฝ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ธ ์ˆ˜๋Š” ๋บ€ ์ˆ˜๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

ํ’€์ด

function solution(left, right) {
    let answer = 0

    for(let i = left; i <= right; i++) {
        let count = 0;

        for(let j = 1; j <= i; j++) {
            if(i % j === 0) {
                count++
            }
        }

        if (count % 2 === 0) {
            answer += i;
        } else {
            answer -= i;
        }    
    }

    return answer
}

๐Ÿ‘€ ๋‹ค๋ฅธ ์‚ฌ๋žŒ ํ’€์ด

function solution(left, right) {
    var answer = 0;
    for (let i = left; i <= right; i++) {
        if (Number.isInteger(Math.sqrt(i))) {
            answer -= i;
        } else {
            answer += i;
        }
    }
    return answer;
}

Last updated