Open
Conversation
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
팩토리얼
문제: 0 이상의 정수 N이 주어질 때 N! 출력하기
배운 점:
반복문 내부에서 선언한 변수는 매번 초기화되므로 결과를 누적하려면 반복문 외부에 선언해야 한다는 것을 배웠습니다. 또한 for문의 반복 변수 i를 실제 계산에 활용하는 방법을 익혔습니다.
어려웠던 점:
처음에는 repeat와 고정값(N-1)으로 접근했는데 매번 같은 값만 계산되는 문제가 있었습니다. 결과를 저장할 변수를 어디에 선언해야 하는지, for문의 i를 어떻게 곱셈에 사용하는지 파악하는 데 시간이 걸렸습니다.
해결 방법:
반복문으로는
var result = 1을 외부에 선언하고for (i in 1..n)으로 i를 직접 곱하는 방식으로, 재귀로는n * factorial(n-1)로 자기 자신을 호출하는 방식으로 구현했습니다.