Factorial refers to the multiplication of all integers from 1 to a positive integer n, that is, n! = 1 × 2 × 3 × … × n. Here is the Python code to calculate the hierarchy:
def factorial(n):
"""
计算阶层
:param n: 正整数
:return: n的阶层
"""
if n == 1 or n == 0:
return 1
else:
return n * factorial(n-1)
Usage example:
print(factorial(5)) # 输出120,即5的阶层