当前位置:   article > 正文

python编程求n的阶乘_使用Python编程的阶乘

python设计算法求解正整数n的阶乘

python编程求n的阶乘

Before we start implementing factorial using Python, let us first discuss what factorial of a number implies.

在开始使用Python实现阶乘之前,让我们首先讨论数字阶乘的含义。

Theoretically, the factorial of a number is defined as the product of all positive integers less than or equal to the number. Certainly, ‘n!’ represents the factorial of an integer ‘n’. As an example, let us look at the factorial of the number 6,

从理论上讲,数字的阶乘定义为所有小于或等于该数字的正整数的乘积。 当然, “ n!” 代表整数'n'的阶乘。 例如,让我们看一下数字6的阶乘

6! = 6 * 5 * 4 * 3 * 2 * 1

6! = 6 * 5 * 4 * 3 * 2 * 1

The following techniques could be followed to determine the factorial of an integer.

可以遵循以下技术确定整数的阶乘。

  1. Using Loop

    使用循环
  2. Using Recursive function call

    使用递归函数调用
  3. Using predefined function ‘factorial()’ from the math module

    使用数学模块中的预定义函数'factorial()'

在Python中使用循环 (Using Loop in Python)

The below-mentioned code illustrates how we can calculate the factorial of a given number using for loop in Python programming.

下面提到的代码说明了如何在Python编程中使用for 循环来计算给定数字的阶乘。

  1. n=9
  2. fact=1
  3. for i in range(2,n+1):
  4. fact=fact*i
  5. print("The factorial of ",n," is: ",fact)

Output:

输出:

  1. The factorial of 9 is: 362880

在Python中使用递归函数调用 (Using Recursion function call in Python)

Similarly, we can also calculate the factorial of a given number using a Recursive function. Let us see how

同样,我们也可以使用递归函数来计算给定数字的阶乘。 让我们看看

  1. n=9
  2. def fact(n):
  3. if(n==1 or n==0):
  4. return 1
  5. else:
  6. return n*fact(n-1)
  7. print("The factorial of ",n," is: ",fact(n))

Output

输出量

  1. The factorial of 9 is: 362880

For a clear understanding of functions and recursion, one can refer to

为了清楚地了解函数递归 ,可以参考

Python Function and Arguments
Python Recursion Function

Python函数和参数
Python递归函数

在Python中使用Math模块中的factorial()方法 (Using the factorial() method from the math module in Python)

The math module provides a simple way to calculate the factorial of any positive integer. Certainly, the module comes with a pre-defined method ‘factorial()’ which takes in the integer as an argument and returns the factorial of the number. Let’s take a look at how we can use the pre-defined method and consequently find the factorial. The code given below depicts how the method ‘factorial()‘ can be used

数学模块提供了一种简单的方法来计算任何正整数的阶乘。 当然,该模块带有预定义的方法'factorial()' ,该方法将整数作为参数并返回数字的阶乘。 让我们看一下如何使用预定义方法并因此找到阶乘。 下面给出的代码描述了如何使用方法' factorial() '

  1. import math
  2. n=9
  3. print("The factorial of ",n," is: ",math.factorial(n))

Output:

输出:

  1. The factorial of 9 is: 362880

Furthermore, in the case of all of the above-mentioned techniques, we have used a pre-defined value of the integer ‘n’. Also making ‘n’ a user input is possible. This could be easily achieved by substituting the line ‘n=9’ with:

此外,在所有上述技术的情况下,我们使用了整数“ n”预定义 也可以使用户输入为“ n” 。 通过将行“ n = 9”替换为:

  1. n=int(input("Enter the number for calculating factorial"))

The Python input function is covered in further detail in one of our previous articles.

我们之前的一篇文章进一步详细介绍了Python输入函数

References:

参考文献:

https://stackoverflow.com/questions/5136447/function-for-factorial-in-python

https://stackoverflow.com/questions/5136447/function-for-factorial-in-python

https://stackoverflow.com/questions/20604185/find-the-best-way-for-factorial-in-python

https://stackoverflow.com/questions/20604185/find-the-best-way-for-factorial-in-python

翻译自: https://www.journaldev.com/34688/factorial-using-python-programming

python编程求n的阶乘

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/212742?site
推荐阅读
相关标签
  

闽ICP备14008679号