阶乘是数学中的一个概念,用来表示一个正整数的乘积。在Python中,可以使用math.factorial()函数来计算阶乘。math.factorial()函数接受一个正整数作为参数,并返回该数字的阶乘。
使用math.factorial()函数计算阶乘
要使用math.factorial()函数,需要导入math模块:
import math
可以使用math.factorial()函数来计算阶乘:
math.factorial(n) # n为正整数
下面是一个示例:
import math # 计算5的阶乘 print(math.factorial(5)) # 输出:120
可以看到,math.factorial()函数计算出5的阶乘是120。
math.factorial()函数也可以用来计算大数字的阶乘,比如计算100的阶乘:
import math # 计算100的阶乘 print(math.factorial(100)) # 输出:93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
math.factorial()函数还可以用来计算小数的阶乘,比如计算0.5的阶乘:
import math # 计算0.5的阶乘 print(math.factorial(0.5)) # 输出:1.7724538509055159
可以看到,math.factorial()函数可以用来计算任意正整数或小数的阶乘。
- math.factorial()函数接受一个正整数或小数作为参数,并返回该数字的阶乘。
- 要使用math.factorial()函数,需要先导入math模块。
- math.factorial()函数可以用来计算任意正整数或小数的阶乘。