Python练习实例025

问题:求1!+2!+3!+...+20!的和。

#! /usr/bin/env python3
# -*- coding:utf-8 -*-

# Author   : Ma Yi
# Blog     : http://www.cnblogs.com/mayi0312/
# Date     : 2020-06-19
# Name     : demo025
# Software : PyCharm
# Note     : 求1!+2!+3!+...+20!的和。


# 入口函数
if __name__ == '__main__':
    result = 0
    temp_list = []
    for i in range(20):
        fac = 1
        for j in range(i + 1):
            # n!
            fac *= (j + 1)
        temp_list.append("%d!" % (i + 1))
        result += fac
    print("+".join(temp_list) + "=%d" % result)

运行结果:

1!+2!+3!+4!+5!+6!+7!+8!+9!+10!+11!+12!+13!+14!+15!+16!+17!+18!+19!+20!=2561327494111820313

猜你喜欢

转载自www.cnblogs.com/mayi0312/p/13161475.html