-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 재귀함수 - 종료조건을 항상 명시해야 한다. | ||
# 재귀함수는 컴퓨터 내부에서 스택 자료구조를 이용하여 실행된다. | ||
|
||
# 100번째 종료됨 | ||
def recursive_function(i): | ||
if i == 100: | ||
return | ||
recursive_function(i+1) | ||
print(i) | ||
|
||
recursive_function(20) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# 반복을 사용하여 구현 | ||
def factorial_iterative(n): | ||
result = 1 | ||
for i in range(1, n+1): | ||
result *= i | ||
return result | ||
|
||
# 재귀를 사용하여 구현 | ||
def factorial_recursive(n): | ||
if n<=1: | ||
return 1 | ||
return n * factorial_recursive(n-1) | ||
|
||
print("반복", factorial_iterative(5)) | ||
print("재귀", factorial_recursive(5)) |