λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

ν”„μ—” 곡뢀/🫧 μ•Œκ³ λ¦¬μ¦˜ 곡뢀

[μ•Œκ³ λ¦¬μ¦˜ || ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€] μ•½μˆ˜μ˜ κ°œμˆ˜μ™€ λ§μ…ˆ - μ•½μˆ˜μ˜ κ°œμˆ˜μ™€ 제곱근의 상관관계

728x90

πŸŽ‰ μ•½μˆ˜μ˜ 개수 

=> 제곱근이 μ •μˆ˜λ©΄, μ•½μˆ˜μ˜ κ°œμˆ˜λŠ” ν™€μˆ˜λ‹€!!

Number.isInteger(Math.sqrt(i))

 

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;
}

Number 의 isInteger. ν•¨μˆ˜λ‘œ i 의 제곱근이 μ •μˆ˜μΈμ§€ νŒλ³„ν•΄μ£ΌλŠ” 방식

 

 

λ¬Όλ‘  λ‚˜λŠ” 이걸 λͺ°λžμ–΄μ„œ

function solution(left, right) {
    const countingMeasure = (num) => {
        const measure = [];
        for (let j = 1; j <= num; j++) {
            if (num % j === 0) measure.push(j)
        }
        return measure.length;
    }
    let result = 0;
    for (let i = left; i <= right; i++) {
        if (countingMeasure(i) % 2 == 0) result += i;
        if (countingMeasure(i) % 2 != 0) result -= i;
    }
    return result;
}

 

μ΄λ ‡κ²Œ μ•½μˆ˜μ˜ 개수λ₯Ό 직접 μ„Έμ„œ ν’€μ—ˆλ‹€~~

728x90
λ°˜μ‘ν˜•