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 循环来计算给定数字的阶乘。


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

Output:

输出:


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

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


n=9
def fact(n):if(n==1 or n==0):return 1else:return n*fact(n-1)print("The factorial of ",n," is: ",fact(n))

Output

输出量


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() '


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

Output:

输出:


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”替换为:


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的阶乘

python编程求n的阶乘_使用Python编程的阶乘相关推荐

  1. python求n的阶乘并输出身份信息_python编程求n的阶乘_使用Python编程的阶乘

    python编程求n的阶乘 Before we start implementing factorial using Python, let us first discuss what factori ...

  2. 从python开始学编程pdf 解压密码_从Python开始学编程PDF高清完整版网盘免费分享...

    提取码:szq0 image 内容简介 · · · · · · 改编自Vamei博客的<Python快速教程>.本书以Python为样本,不仅介绍了编程的基本概念,还着重讲解编程语言的主流 ...

  3. python编程能有什么用_揭秘python都能做什么?

    Python作为一种功能强大,并井且简单易学的编程语言而广受好评,那么Python都能做么?概据起来有以下几个方面. Wob开发使用Python的二个基本应用就是进行Web开发,在国内,大一些的使用P ...

  4. 用python的turtle画等边三角形讲解_通过Python turtle绘制等边三角形谈编程思维和逻辑...

    相信三角形对于绝大数人来说,是再熟悉不过的一个几何图形了,并且在现实生活和工作中也经常遇见,如果要画的话,相信很多人也会说,简单的很啊,画三条相等的线段,每两个线段的夹角为60度就可以了啊,也许我们借 ...

  5. 树莓派python编程入门先学什么_树莓派Python编程入门与实战

    树莓派Python编程入门与实战 编辑 锁定 讨论 上传视频 本词条缺少概述图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 树莓派是一个只有信用卡大小的裸露电路板,它也是一个运行开源Lin ...

  6. python发行版是什么意思_致 Python 初学者

    当下是一个喧嚣.浮躁的时代.我们总是被生活中大量涌现的热点所吸引,几乎没有深度阅读和思考的时间和机会.我始终认为,学习是需要沉下心来慢慢钻研的,是长期的:同时,学习不应该被赋予太多的功利色彩.一个Py ...

  7. python以运行效率高著称吗_提升Python程序运行效率的6个方法

    Python是一个很酷的语言,因为你可以在很短的时间内利用很少的代码做很多事情.不仅如此,它还能轻松地支持多任务,比如多进程等.Python批评者有时会说Python执行缓慢.本文将尝试介绍6个技巧, ...

  8. python源码多平台编译_提升Python程序运行效率的6个方法

    Python是一个很酷的语言,因为你可以在很短的时间内利用很少的代码做很多事情.不仅如此,它还能轻松地支持多任务,比如多进程等.Python批评者有时会说Python执行缓慢.本文将尝试介绍6个技巧, ...

  9. python中numpy数组的合并_基于Python中numpy数组的合并实例讲解

    基于Python中numpy数组的合并实例讲解 Python中numpy数组的合并有很多方法,如 - np.append() - np.concatenate() - np.stack() - np. ...

最新文章

  1. Redis概述和基础
  2. Java技术——Iterator和Enumeration的不同
  3. db 文件 加密_有人说Kettle 数据库JNDI方式数据库密码不能加密,搞他!
  4. css超出一行添加省略号属性
  5. 一种storyboard+swift实现页面跳转的方法
  6. python paramiko sftp_Python学习—paramiko模块实现简单的ssh与sftp
  7. 《朝花夕拾》金句摘抄(三)
  8. xp系统目前禁用索引服务器,WinXP系统中可以被禁用的服务对照表
  9. corba的兴衰_数据科学薪酬的兴衰
  10. matlab如何使音频文件声音变大_如何用录音软件AU2020给音频消除噪音?只需简单几步你也可以办到...
  11. 给 layui upload 带每个文件的进度条, .net 后台代码
  12. 阿里 P7 到底该具备什么样的能力?
  13. shell蚂蚁森林_和“蚂蚁森林”的融合
  14. React中获取地址栏传参
  15. 木瓜移动创始人沈思专访——从平台到孵化器
  16. 用 C 语言编写的程序被称为,用c语言编写的程序被称为
  17. Maven项目在new module后,pom文件显示为Ignored pom.xml
  18. c语言有多难?一个新手刚学c语言的无奈
  19. 独作论文登数学顶刊,浙大女神校友再被提起:32岁在贵州深山投稿,一家出两位院士...
  20. python时区、时差、时间差转换——datetime.timedelta类介绍

热门文章

  1. 对DataGridView中的DataGridViewComboBoxColumn有了一点点体会
  2. [转载] python iter( )函数
  3. [转载] python cmp函数比较字典_Python 字典(Dictionary) cmp()方法
  4. [转载] 【python】Python中*args和**kwargs的区别(在Python中如何使用可变长参数列表)
  5. 设计模式——命令模式
  6. zabbix--基础概念及原理
  7. 11.CSS border边框
  8. (第十二周)Bug修正报告
  9. iOS开发日记9-终端命令
  10. Android中GridView实现互相添加和删除