Python练习题 047:Project Euler 020:阶乘结果各数字之和
2024-09-06 15:45:43
本题来自 Project Euler 第20题:https://projecteuler.net/problem=20
'''
Project Euler: Problem 20: Factorial digit sum
n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is
3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!
Answer: 648
''' n = 100
fac = 1 #初始化阶乘结果
while n >= 1:
fac *= n
n -= 1 # 提取出阶乘结果的每个数字,形成列表lst
lst = [int(i) for i in str(fac)] res = 0 #初始化相加结果
for i in range(len(lst)):
res += lst[i]
print(res)
这题也容易,让先算出阶乘100的结果,然后把这结果的每个数字相加即可。
我想,应该是要练习递归阶乘吧,但我觉得用循环也挺方便的啊,就是很讨厌递归函数,总记不住写法,唉……
最新文章
- 时间格式转换—将后台返回的/Date(1448954018000)/格式转换为正常的时间格式
- sql小技巧
- Unicode 互转
- bzoj 2768: [JLOI2010]冠军调查
- iOS开发基础知识:Core Animation(核心动画)
- oracle以web方式登录EM、ISQLPlus
- 解决win7 中source insight没有courier new字节的问题
- DFT 展开式和 FFT推导
- U盘安装VMware ESXi 6.0
- public static void main(string[] args)解释
- python+selenium自动化软件测试(第12章):Python读写XML文档
- Scheme N皇后
- text-shadow 详解及示例
- lr12脚本参数化
- Systemd 教程
- BZOJ.2034.[2009国家集训队]最大收益(二分图匹配 贪心)
- GIT无法自动忽略YellowRV1.1.uvgui.Administrator文件的解决方法
- CreateProjectFormat——初始项目目录格式
- 软件安装的list(0918)
- 放弃winform的窗体吧,改用html作界面,桌面应用程序UI的新的开发方式。