⚡
Factorial
Factorial보통 함수 +20pts
Problem
Write a function that calculates the factorial of a number.
Examples
Input:
factorial(5)Output:
120💡 5! = 5 × 4 × 3 × 2 × 1 = 120
Input:
factorial(0)Output:
1💡 0! = 1 (정의)
Explanation
이 문제는 **재귀 함수**의 기본 개념을 학습합니다. 팩토리얼은 재귀의 대표적인 예시입니다. **팩토리얼의 수학적 정의** - n! = n × (n-1) × (n-2) × ... × 1 - 0! = 1 (수학적 정의) - 1! = 1 **재귀의 핵심 요소** 1. **기저 조건(Base Case)**: 재귀를 멈추는 조건 - n <= 1일 때 1을 반환 2. **재귀 호출(Recursive Case)**: 자기 자신을 호출 - n * factorial(n - 1) **factorial(5)의 실행 과정** ``` ...
View detailed explanation →Key Concepts
재귀 함수 기저 조건 재귀 호출 팩토리얼 수학
Time: O(n) Space: O(n)
solution.js
Ctrl + Enter
Run tests to see results here.