📝
Power of Two
Power of Two - Explanation
입문 기초 문법 O(1) O(1)
Problem Summary
Write a function that returns 2 raised to the given power.
Go to Problem →Detailed Explanation
이 문제는 **거듭제곱 연산**을 학습합니다. 컴퓨터 과학에서 2의 거듭제곱은 특별히 중요합니다. **Math.pow() 함수** `Math.pow(base, exponent)`는 base의 exponent 거듭제곱을 반환합니다: - Math.pow(2, 3) = 2³ = 8 - Math.pow(2, 0) = 2⁰ = 1 **거듭제곱 연산자 (**)** ES2016부터 `**` 연산자를 사용할 수 있습니다: - `2 ** 3` = 8 - `2 ** 10` = 1024 **2의 거듭제곱이 중요한 이유** 컴퓨터는 2진법을 사용하므로 2의 거듭제곱이 자주 등장합니다: - 2¹⁰ = 1024 (1KB) - 2²⁰ = 1,048,576 (1MB) **0제곱은 1** 수학적 정의에 따라 어떤 수의 0제곱도 1입니다: - 2⁰ = 1 - 10⁰ = 1 **비트 연산으로 계산** `1 << n`도 2의 n제곱을 계산합니다 (더 빠름): - 1 << 3 = 8 - 1 << 10 = 1024
Solution Code
solution.js
function powerOfTwo(n) {
return Math.pow(2, n);
}Key Concepts from This Problem
1. Math.pow 함수
2. 거듭제곱 연산자 (**)
3. 2의 거듭제곱
4. 0제곱의 정의
Common Mistakes
✗ ^는 거듭제곱이 아니라 비트 XOR 연산자입니다
✗ 음수 지수도 가능합니다 (2^-1 = 0.5)
✗ 매우 큰 지수는 Infinity가 될 수 있습니다
Hints
Hint 1: Math.pow 또는 ** 연산자를 사용하세요.
Complexity Analysis
Time Complexity
O(1)
Constant time regardless of input size
Space Complexity
O(1)
Uses almost no additional memory
Related Tags
#기초 #Math #거듭제곱