jaegomhoji 2022. 1. 25. 19:24

* 팩토리얼 : n! , 1부터 0의 정수 n까지 모두 곱한 것 

* 정리는 노트에 기록함

 

* jupyter notebook ( python 3 )

* 조건문 , 재귀함수 활용 방법

 

for문과 while문을 사용해서 출력
재귀 함수를 사용하여 출력

 

math 함수 사용

 

<< 복붙용 코드 >>

 

# 반복문 이용 
# for 문 

inputN = int(input("n의 팩토리얼:"))

result = 1
for n in range ( 1, inputN+1 ):
    result *= n

print("{}의 팩토리얼:{}".format(n,result))

 

# while 문 

inputN = int(input("n의 팩토리얼:"))

n = 1
result = 1

while n <= inputN:
    result *= n
    n += 1

print("{}의 팩토리얼:{}".format(inputN,result))

 

# 재귀 함수를 이용하는 경우 

inputN = int(input("n의 팩토리얼:"))

# inputN이 함수로 들어가서 n*factorialFun(n-1)을 호출 >>> if n==1 즉, 2*factfun(1) 일때 재귀함수가 종료, 1이 호출되며 루프 종료 

def factorialFun(n):
    if n==1: return 1
    
    return n *factorialFun(n-1)

print("{}의 팩토리얼:{}".format(inputN,factorialFun(inputN)))

 

* math 함수

 

import math 

math.factorial(inputN)