Python Math.factorial()方法
在Python中,我们使用math.factorial()
方法来返回一个数字的阶乘。方法math.factorial()
是在Python 2.6版本中引入的,是Python中理论和表示函数的一部分。
这个方法接收一个正整数值,并返回指定正整数的阶乘。当我们向factorial()
方法传递一个非正数或小数时,该方法返回ValueError
。
TypeError
当参数甚至不是一个数字时就会发生。
一个数字的阶乘是指从指定的正数开始到1的所有乘法之和,其中包括范围内的所有整数。例如,7的阶乘是7x6x5x4x3x2x1=5040。
Pythonmath.factorial()
方法的语法
math.factorial(number)
参数
number |
这是一个必需的参数,一个正的整数值。这个参数不允许有负数或小数的值。 |
返回
该方法返回阶乘的正整数值。
示例代码:使用math.factorial()
方法
要在 Python 中使用factorial()
方法,我们必须导入math
模块。math
模块提供的方法之一是math.factorial()
方法,它只接受正整数值并返回正整数值。
这个方法不接受任何其他类型的数据值。
# import math module to use the math.factorial() method
import math
# returns the factorial of a number
def evaluate_factorial(number):
return math.factorial(number)
num = 13
print ("The factorial of 13 is ", end ="")
print (evaluate_factorial(num))
输出:
The factorial of 13 is 6227020800
示例代码:使用math.factorial()
方法时出现ValueError
异常的原因
我们将在math.factorial()
方法中传递正的整数值作为参数。当我们向该方法传递一个非正值时,factorial()
方法会返回ValueError
异常。
我们可以用try
和except
语句来处理这种异常。
import math
def evaluate_factorial(number):
return math.factorial(number)
# not a positive integer number
num = 13.1
# exception caught when the value of num is decimal
try:
print ("The factorial of 13.1 is ", evaluate_factorial(num))
except ValueError as ve:
print(f'You entered a {num} number, which is not a positive integer number.')
输出:
You entered a 13.1 number, which is not a positive integer number.
示例代码:使用math.factorial()
方法时出现TypeError
异常的原因
与上面的例子类似,当数据类型不是数字作为参数时,math.factorial()
方法返回TypeError
异常。我们也可以在源代码中处理TypeError
异常。
import math
def evaluate_factorial(number):
return math.factorial(number)
# invalid string type for factorial() method
word = "Hello"
# exception caught when the parameter type is a string
try:
print ("The factorial of 33 is ", evaluate_factorial(word))
except TypeError as ve:
print(f'You entered a {word} word, which is a string.')
输出:
You entered a Hello word, which is a string.
示例代码:使用math.factorial()
方法来寻找互换性
我们使用math.factorial()
方法来解决数学中的不同公式。该公式为:n!/(n-r)!
,其中n
是集合中的总项,r
是来自排列组合的项。
该公式用于寻找一个数字的排列组合。该公式中r
的值不能大于或等于n
的值。
import math
# returns result for formula n!/(n-r)!
def permutation(n, r):
return math.factorial(n) / math.factorial(n-r)
# inputs 'n' and 'r' are needed for the formula
n = int(input("First input: Enter value of n: "))
while(True):
r = int(input("Second input: Enter value of r: "))
if(r>=n):
print("The value of r cannot be greater or equal to n. Input again")
continue
break
print("The result of n! / (n-r)! is",permutation(n, r))
输出:
First input: Enter value of n: 5
# first input cannot be greater than the second input
Second input: Enter value of r: 7
The value of r cannot be greater or equal to n. Input again
Second input: Enter value of r: 3
The result of n! / (n-r)! is 60.0
示例代码:math.factorial()
方法的简便性
这个方法通过接收一个必要的参数并返回阶乘,提供了一个单一语句的简便性。如果我们不在math
模块中使用这个方法,我们需要写很多语句。
# math.factorial does this in a single statement
def factorial(num):
if num==0 or num==1 :
return 1
else :
return num * factorial(num-1)
print("The factorial of 4 is",factorial(4))
输出:
The factorial of 4 is 24
示例代码:math.factorial()
方法的运行时复杂度
在阶乘中,数字被指定的数字乘以1,包括范围内的所有整数。对于一个更大的数字,阶乘会变得非常大,并以指数形式增长。
因此,当有一个较大的乘法时,总是需要计算大O的符号。由于这个原因,程序的结构是影响运行时间差异的一个重要因素。
我们写了两个基本的factorial()
程序,参数相同,但程序逻辑不同,以区分运行时间的复杂程度。
程序1:
import math
import time
begin = time.time()
def factorial(a):
if a==0 or a==1:
return 1
else:
return a*factorial(a-1)
print("Factorial:",factorial(30))
stop = time.time()
print("The time elapsed for the Recursive method is " + str(stop-begin))
输出:
Factorial: 265252859812191058636308480000000
The time elapsed for the Recursive method is 0.000997781753540039
程序2:
import math
import time
begin = time.time()
# logic is different in the second program for finding factorials.
def fact(a):
facto = 1
for number in range(2, a + 1):
facto *= number
return facto
print("Factorial:",fact(30))
stop = time.time()
print("The time elapsed for the iterative method is " + str(stop-begin))
输出:
# same factorial but the difference in the run time of both programs.
Factorial: 265252859812191058636308480000000
The time elapsed for the iterative method is 0.00099945068359375