Python语言基础与应用-北京大学-陈斌-P40-39-基本扩展模块/上机练习:计时和文件处理-给算法计时-上机代码

Python语言基础与应用-北京大学-陈斌-P40-39-基本扩展模块/上机练习:计时和文件处理-给算法计时-上机代码

上机代码:

# 基本扩展模块训练 给算法计时
def factorial(number): # 自定义一个计算阶乘的函数
    i = 1
    result = 1 # 变量 result 用来存储每个数的阶乘结果
    sum_result = 0 # 变量 sum_result 用来存储每个数的阶乘的结果之和
    while i <= number:
        result = result * i
        sum_result += result
        i += 1
        
    return(sum_result)
    
        
import time

t1 = time.time()
print("开始运行程序的系统时间:",t1)

i = 1
temp = 0
while i <= 100:
    temp = factorial(i)
    i += 1    

print("%d 阶乘之和的结果是: %d" % (i-1,temp))
    
t2 = time.time()
print("结束运行程序的系统时间:",t1)

print("程序运行时长:" )
print(t2-t1)

运行结果:

>>> %Run test.py
开始运行程序的系统时间: 1710338481.1418793
100 阶乘之和的结果是: 94269001683709979260859834124473539872070722613982672442938359305624678223479506023400294093599136466986609124347432647622826870038220556442336528920420940313
结束运行程序的系统时间: 1710338481.1418793
程序运行时长:
0.0010008811950683594
>>> 

如果设置阶乘累加到1000(n=1~1000),结果如下:

>>> %Run test.py
开始运行程序的系统时间: 1710339585.5457308
1000 阶乘之和的结果是: 402790050531223457680670558292183044930441094551528966419334642920075902370082550849878938011122874840322664395727767319128483846892726398968790560901035018977865543415443376965144616549510538851077440410165819782073475583082079396504111380884916810768467471317168913183948275897490803646698793846190852594417739671786914563472162093180956284720843607311204509397095907530114810381703803860648328656344389023077975647748628886817466804724858587747840936784462436173977429991182753207302348016538494642001301432395279726745538178727708503090870745742249581124437084158031806471602854863650302741825130214652788342621283870175833906572656890104432413095171095723550851047360182700817666447833450097928957386876271765554137407111747666512309980678539886786878262902584539118231519316835523885400137767203426696953126303292828224235349134630280084480064650497764755473824102259453127730077777531063115138764952527597098445380046443997540925880086936317933421768424925084707414812932329166745886234137226923024813461900363225999464524935227947647167947434411637139203517404371214232998239493954562323327076903915931443305369962856388265027346155623332543302943446674383336900484885947609695640139797208838438493998636482572950930777919644406154624084228042985270573774543689565298092301062689807689071120317578510142181892798247199071452022029172707400688758980305702923813291544851703914211657597582497807598793035565731652297925769886549168450867091163868402442018371341022143349040941897422377931540131712376088451926646802255265721820332564453961091792448710886674897251899281432897966817352381951694370184383299109654078410356489144960977274480320655205474842571310858767154579079644215514199003832599232631020684528203955813111140473074686810426051322892063650122841004997271652977702547274337834459757112850838135183696311712170990224739928697694772853140476802033735594574707189941820656793611572800671948131128109558012358679446259496244143021997936801273888393102412918463505832701613704464047394891693567824946365095976820092895872874833067345472399549213736568277881574159120670765215962463587184455433922702506813953637742383768964226648365610070168839324749896910992390804658988479107678562550844858514512762455637677863067821690083222834405613388709119188719479181528190836888908152845729021513491658586509889455613570273121541638995145894561079999213500076101248781500642979091198044736325729107144579768273776743716439557581557253878204647391176582851167312182026188795156620056856503340092247479478684738621107994804323593105039052556442336528920420940313
结束运行程序的系统时间: 1710339585.5457308
程序运行时长:
0.11602449417114258
>>> 

如果设置阶乘累加到10000(n=1~10000),系统会报错,结果如下:

>>> %Run test.py
开始运行程序的系统时间: 1710339823.601155
Traceback (most recent call last):
  File "C:\Users\Admin\Desktop\test.py", line 25, in <module>
    print("%d 阶乘之和的结果是: %d" % (i-1,temp))
ValueError: Exceeds the limit (4300) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit
>>> 

ValueError: Exceeds the limit (4300) for integer string conversion; use sys.set_int_max_str_digits() to increase the limit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qslife

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值