python计算n的阶乘的方法代码
作者:爱喝马黛茶的安东尼 时间:2023-08-20 07:33:00
整数的阶乘(英语:factorial)是所有小于及等于该数的正整数的积,0的阶乘为1。即:n!=1×2×3×...×n。
首先导入math模块,然后调用factorial()函数来计算阶乘。
1 math.factorial(x)
import math
value = math.factorial(x)
2. reduce函数
def factorial(n):
return reduce(lambda x,y:x*y,[1]+range(1,n+1))
3. 递归实现
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
标签:python,阶乘
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP 判断是否有中文的代码
2011-04-15 11:07:00
vue中对象数组去重的实现
2024-04-27 16:18:27
![](https://img.aspxhome.com/file/2023/0/132920_0s.png)
如何基于python3和Vue实现AES数据加密
2023-08-08 23:47:37
asp.net FindControl方法误区和解析
2024-06-05 09:28:08
详解pyinstaller selenium python3 chrome打包问题
2023-08-12 08:26:40
![](https://img.aspxhome.com/file/2023/0/120410_0s.png)
Python一行代码识别发票并保存Excel示例详解
2023-07-28 01:06:50
![](https://img.aspxhome.com/file/2023/8/107878_0s.png)
asp之日期和时间函数示例
2008-04-13 06:50:00
python中三种输出格式总结(%,format,f-string)
2022-06-11 16:34:40
![](https://img.aspxhome.com/file/2023/6/75356_0s.png)
javascript for-in有序遍历json数据并探讨各个浏览器差异
2024-05-25 15:18:17
![](https://img.aspxhome.com/file/2023/7/123717_0s.png)
月影:JavaScript王者归来
2008-08-05 17:18:00
![](https://img.aspxhome.com/file/UploadPic/20088/5/200885172229312s.jpg)
图文详解laravel多对多关联模型
2023-06-12 22:21:34
![](https://img.aspxhome.com/file/2023/4/55544_0s.png)
php 解压zip压缩包内容到指定目录的实例
2024-04-18 09:28:47
python批量处理打开多个文件
2022-10-21 05:26:47
![](https://img.aspxhome.com/file/2023/0/113030_0s.png)
pytorch 自定义参数不更新方式
2021-11-11 01:55:55
PHP中quotemeta()函数的用法讲解
2023-06-08 04:04:24
![](https://img.aspxhome.com/file/2023/7/55377_0s.png)
Python数据库的连接实现方法与注意事项
2024-01-14 10:51:54
mysql 5.7.5 m15 winx64安装配置方法图文教程
2024-01-14 10:35:04
go实现脚本解释器gscript
2023-10-12 00:49:39
![](https://img.aspxhome.com/file/2023/6/103036_0s.gif)
基于django ManyToMany 使用的注意事项详解
2022-10-16 00:29:33
Python实现将HTML转成PDF的方法分析
2023-06-19 18:59:37
![](https://img.aspxhome.com/file/2023/2/99552_0s.png)