Python 程序查找数字的阶乘
使用 for 循环计算数字的阶乘
让我们来看一个 Python 代码示例,该代码接受一个正整数作为输入,以确定正整数的阶乘。在以下代码中,循环从 1 开始,然后乘以实际要确定其阶乘的数字之前的每个数字。
以下 Python 代码说明了如何使用循环计算阶乘函数。
Python 代码
print ("Input a number") factorialIP = int (input ()) ffactor23 = 1 for j in range (1, factorialIP+1): ffactor23 = ffactor23 * j print ("The factorial of the number is “, ffactor23)
输出
Input a number 4 The factorial of the number is 24
上述 Python 程序仅接受正数输入,并且不对负数进行检查。在此程序中,当 j 等于 1 时,因子为 1。当 j 为 2 时,因子乘以 2,并将一直执行此操作,直到 j 到达 4,最终得到 24。
使用 IF...else 语句计算数字的阶乘
以下 Python 代码说明了如何使用函数计算阶乘。让我们来看下面的 Python 代码,该代码接受正整数作为输入,以确定正整数的阶乘。
在之前的 Python 代码中,没有应用对负数的检查,这使得阶乘函数不完整,并且如果输入负数,则可能导致错误消息。
在给定的代码中,循环从 1 开始,然后乘以实际要确定其阶乘的数字之前的每个数字,该函数还对负数进行检查。
Python 代码
print("Enter a number for the purpose of determining factorial") factorialIP = int(input()) def factorial(factorialIP): if factorialIP < 0: print ('Factorial does not exist') factor=0 return factor elif factorialIP == 0: factor=1 return factor print(factor) else: factor = 1 for j in range (1, factorialIP+1): factor = factor * j return factor print ("The factorial of the number is ", factorial(factorialIP))
输出
1) Enter a number to determine factorial -4 Factorial does not exist The factorial of the number is 0 2) Enter a number to determine factorial 4 Factorial does not exist The factorial of the number is 24
上述 Python 程序查找数字的阶乘 仅接受正数输入,并且使用 Python 的 if 和 else 语句确实对负数进行了检查。在此程序中,当 j 等于 1 时,因子为 1。当 j 为 2 时,因子乘以 2,并将一直执行此操作,直到 j 到达 4,最终得到 24。
使用递归计算数字的阶乘
以下 Python 代码说明了如何使用递归计算阶乘。让我们来看下面的 Python 代码,该代码接受正整数作为输入,以确定正整数的阶乘。在此示例中,递归函数确定阶乘数。
Python 代码
print("Enter a number for the purpose of determining factorial") def factorial(num2): if num2 < 0: return 'Factorial does not exist' elif num2 == 0: return 1 else: return num2 * factorial(num2-1) number1 = int(input()) print("The factorial of the number is",factorial(number1))
输出: –
Enter a number for the purpose of determining factorial 4 The factorial of the number is 24
递归可以解释为一个概念,即 Python 模块中调用的函数可以一遍又一遍地调用自身。它运行直到 Python 模块中存在的 Python 条件得到满足,此时调用的函数被赋予一个值。
在上述 Python 程序中,函数 number def factorial 不断地递归调用自身,直到数字达到零。一旦数字达到零,它将数字初始化为 1,从而结束递归。
使用 math.factorial() 计算数字的阶乘
以下 Python 代码说明了如何使用 math.factorial() 计算阶乘,可以通过导入 math 模块来使用它。
此函数不接受负整数,并且在提供浮点数时会引发值错误消息。让我们来看下面的 Python 代码,该代码接受正整数作为输入,以确定正整数的阶乘。
Python 代码
print("Enter a number for computing factorial") import math number1 = int(input()) print("The factorial is as computed comes out to be ") print(math.factorial(number1))
输出: –
Enter a number for computing factorial 4 The factorial, as computed, comes out to be 24
Python 中阶乘程序的算法
让我们举一个例子来说明阶乘的概念。
为了确定 5 的阶乘,请遵循以下步骤: –
5! = 5 x (5-1) x (5-2) x (5-3) x (5-4) 5! =120
此处,5! 表示为 120。
以下图有助于理解计算阶乘的算法,在这种情况下,让我们以 4! 的阶乘为例。
Python 中阶乘的应用
数字的阶乘在数学中有广泛的应用。以下是 Python 的重要应用
- Python 帮助以比其他可用编程语言更快、更高效的方式进行计算,然后打印阶乘。
- Python 代码易于理解,并且可以在不同平台上复制,并且阶乘 Python 程序可以纳入几项数学建模作业中。
摘要
- 数字的阶乘可以描述为,为了确定其阶乘的数字,所有小于或等于该数字的正整数的乘积或乘法。
- 在 Python 中,有三种方法可以计算数字的阶乘。
- 使用 For 循环计算阶乘
- 使用递归计算阶乘。
- 使用用户定义的函数
- 数字的阶乘是为非负整数确定的,结果始终为正整数。
- 根据规则的例外,零的阶乘为 1。
- 数字的阶乘在数学中有广泛的应用。
学习我们的下一个教程,了解 不使用第三个变量交换两个数字