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

[μ•Œκ³ λ¦¬μ¦˜ || ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€] μ •μˆ˜ 제곱근 νŒλ³„

λ˜μžπŸ‹ 2023. 5. 29. 10:58
728x90

λ‚˜μ˜ μ½”λ“œ

function solution(n) {
    return Number.isInteger(Math.sqrt(n)) ? (Math.sqrt(n) + 1) ** 2 : -1;
}

sqrt λ₯Ό μ΄μš©ν•΄μ„œ μ œκ³±κ·Όμ„ κ΅¬ν•˜κ³  이게 integer λ©΄ κ·Έ λ’€μ˜ 계산을 λ°˜ν™˜ν•œλ‹€.

 

μ •μˆ˜μΈμ§€ νŒλ³„ν•˜λŠ” λ‹€λ₯Έ 방법은 1둜 λ‚˜λˆ΄μ„ λ•Œ λ‚˜λ¨Έμ§€κ°€ 0이 λ˜λŠ”μ§€λ₯Ό ν™•μΈν•˜λŠ” 것이닀.

Math.sqrt(n) % 1 == 0

 

 

 

λ„Ή?

γ…Žγ„·γ„·...

function solution(n) {
    return ((x) => Number.isInteger(x) ? (x + 1) ** 2 : -1)(Math.sqrt(n))
}

됐닀~~~

 

μ¦‰μ‹œ μ‹€ν–‰ν•¨μˆ˜

(()=>{})()

 

728x90
λ°˜μ‘ν˜•