阶乘用感叹号(!)表示。我们可以将其定义为小于或等于该数字且大于0的所有正整数的乘积。例如,4的阶乘(4!)意味着 4 x 3 x 2 x 1,等于24。这篇博文将探讨如何编写一个高效的 Python 程序来查找或计算数字的阶乘。
我们从对阶乘算法及其在数学中计算方法的初步了解开始。接下来,我们将向您展示使用For循环、While循环、函数和递归编写Python程序来查找数字阶乘的不同方法。
该编程语言中的数学或math模块有一个factorial()函数,可以直接计算并返回任何正数的结果。您可以通过导入math库来使用此函数。
使用math函数的Python程序查找数字的阶乘
n的阶乘的数学表示为n!。其中n! = n * (n-1) * (n -2) * …….* 1。例如,考虑n = 5。
- 5! = 5 * (5 – 1) * (5 – 2) * (5 – 3) * (5 – 1)
- 5! = 5 x 4 x 3 x 2 x 1 = 120
它用感叹号(!)表示,在这个程序代码中,我们使用内置的math模块的factorial函数来查找数字的阶乘。
import math
a = int(input(" Please enter any Integer : "))
ft = math.factorial(a)
print("The Result of %d = %d" %(a, ft))
在这个例子中,我们使用math prod()函数,它接受迭代值,找到每个值的乘积,并返回数字的阶乘。
import math
num = int(input("Enter any Number : "))
if num < 0:
print("Please Enter Positive Integer Only.")
elif num == 0:
print("The Fact of 0 = 1.")
else:
fact = math.prod(range(1, num + 1))
print(f"The Fact of {num} is {fact}.")

使用For循环查找数字阶乘的Python程序
此代码允许用户输入任何整数。使用给定的值,此程序使用For循环查找数字的阶乘。for循环将从1开始,将当前值与下一个整数相乘,直到达到用户给定的数字。
number = int(input(" Please enter any Number : "))
fact = 1
for i in range(1, number + 1):
fact = fact * i
print("The factorial of %d = %d" %(number, fact))

用户在上面的程序示例中输入的整数是4。请参考math函数、For Loop、While Loop和函数在Python中的文章。
第一次迭代
i = 1, Fact = 1 且 number = 5
Fact = Fact * i;
Fact = 1 * 1 = 1
第二次迭代:i = 2, Fact = 1 且 Number = 5
Fact = 1 * 2 = 2
第三次迭代:i = 3, Fact = 2 且 Number = 5
Fact = 2 * 3 = 6
第四次迭代:i = 4, Fact = 6 且 Number = 5
Fact = 6 * 4 = 24
接下来,i变为5。因此,For循环终止。
For循环的时间复杂度为O(n),空间复杂度为O(1)
上面阶乘程序的代码对于负值返回1。因此,我们需要If else语句来打印负输入的消息。
num = int(input("Enter any Number : "))
fact = 1
if num < 0:
print("Please Enter Positive Integer Only.")
else:
for i in range(1, num + 1):
fact = fact * i
print("The factorial of %d = %d" %(num, fact))
Enter any Number : -9
Please Enter Positive Integer Only.
Enter any Number : 6
The factorial of 6 = 720
使用while循环查找数字阶乘的Python程序
在此程序中,我们只需将for循环替换为while循环来查找数字的阶乘。
value = int(input(" Please enter any Value : "))
fact = 1
i = 1
while(i <= value):
fact = fact * i
i = i + 1
print("The Result of %d = %d" %(value, fact))

使用函数的Python程序查找数字的阶乘
此代码与第一个示例相同。但是,我们使用函数将阶乘逻辑分离。
def calculating(num):
faco = 1
for i in range(1, num + 1):
faco = faco * i
return faco
val = int(input(" Please enter any Value : "))
faco = calculating(val)
print("The Result of %d = %d" %(val, faco))

使用递归查找数字阶乘的Python程序
此代码将用户输入的_值传递给函数。在此递归函数中,此示例程序使用递归函数或递归地查找数字的阶乘。
在此程序的自定义函数中,If Else语句检查整数是否等于0或1。如果条件为TRUE,则函数返回1。如果条件为False,则函数递归地返回 Num * (Num -1)。
def factFind(num):
if num < 0:
print("Please Enter Positive Integer Only.")
elif num == 0:
return 1
else:
return num * factFind(num - 1)
num = int(input("Enter any Num : "))
print(f"Fact of {num} is {factFind(num)}.")

用户输入的_值 = 6。
Fac = num * factFind (num -1);
= 6 * factFind (5)
= 6 * 5 * factFind (4)
= 6 * 5 * 4 * factFind (3)
= 6 * 5 * 4 * 3 * factFind (2)
= 6 * 5 * 4 * 3 * 2 * factFind(1)
= 6 * 5 * 4 * 3 * 2
= 720
递归函数的时间和空间复杂度为O(n)。
使用列表推导式计算阶乘
此程序使用列表推导式查找数字的阶乘。
num = int(input("Enter any Number : "))
if num < 0:
print("Please Enter Positive Integer Only.")
elif num == 0:
print("The Fact of 0 = 1.")
else:
fact = 1
[fact := fact * i for i in range(1, num + 1)]
print(f"The Fact of {num} is {fact}.")
Enter any Number : 5
The Fact of 5 is 120.
使用lambda reduce函数计算阶乘
此Python程序查找数字的阶乘示例使用lambda和reduce函数。
from functools import reduce
num = int(input("Enter any Number : "))
if num < 0:
print("Please Enter Positive Integer Only.")
elif num == 0:
print("The Fact of 0 = 1.")
else:
fact = reduce(lambda a, b: a * b, range(1, num + 1), 1)
print(f"The Fact of {num} is {fact}.")

此示例与上面相同。但是,它使用了lambda递归函数。
from functools import reduce
def factorial(num):
return reduce(lambda a, b: a * b, range(1, num + 1), 1)
num = int(input("Enter any Number : "))
if num < 0:
print("Please Enter Positive Integer Only.")
elif num == 0:
print("The Fact of 0 = 1.")
else:
print(f"The Fact of {num} is {factorial(num)}.")
Enter any Number : 0
The Fact of 0 = 1.