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 代码中,没有应用对负数的检查,这使得阶乘函数不完整,并且在输入负数时容易出现错误消息。

在给定的代码中,循环从一开始,然后将其乘以要确定阶乘的实际数字之前的每个数字,并且该函数还会检查负数。

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,结束递归。

使用数学计算数字的阶乘。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 为例!

阶乘程序算法
阶乘 4 的算法兼图形示例!

阶乘在 Python

数的阶乘在数学中有广泛的应用。以下是阶乘的重要应用 Python:

  • 与其他可用的编程语言相比,python 有助于计算,并且可以更快、更有效地打印阶乘。
  • Python 代码易于理解,可以在不同平台之间复制,并且阶乘 Python 程序可以纳入多个数学模型构建作业中。

总结

  • 一个数的阶乘可以描述为所有等于或小于要确定乘积或阶乘的数的正整数的乘积或乘法。
  • 在 Python 中,有三种方法可以计算数字的阶乘。
    1. 使用 For 循环计算阶乘
    2. 使用递归进行阶乘计算。
    3. 用户定义函数的使用
  • 数的阶乘是针对非负整数确定的,其结果始终为正整数。
  • 根据规则的例外,零阶乘为 1。
  • 数的阶乘在数学中有着广泛的应用。

学习我们的下一个教程 不使用第三个变量来交换两个数字