math 모듈의 factorial 메서드
math 모듈의 factorial 메서드는
숫자의 팩토리얼, 즉 1부터 주어진 숫자까지의
모든 자연수의 곱을 반환합니다. 메서드의 매개변수에는
양수만 전달할 수 있으며, 그렇지 않으면 메서드는 오류를 반환합니다.
구문
import math
math.factorial(숫자)
예제
숫자 5의 팩토리얼을 구해 봅시다:
import math
print(math.factorial(5))
코드 실행 결과:
120
예제
이제 factorial 메서드를
숫자 -4에 적용해 보겠습니다:
import math
print(math.factorial(-4))
코드 실행 결과:
ValueError: factorial() not defined for negative values