更有效的计算方法?
a = 218500000000
s = 6
f = 2
k = 49
d = k + f + s
r = a
i = 0
while (r >= d):
r = r - d
#print ('r = ',r)
i = i+1
#print ('i = ',i)
print (i)
我认为它达到了我的预期,但是计算这么大的数字太慢了,我等了 5 分钟才打印(而 python 使用 100% cpu 来计算..),但它没有。有没有更有效的方法来重写这段代码,以便我可以看到完成需要多少次迭代(i)?
非常感谢
a = 218500000000
s = 6
f = 2
k = 49
d = k + f + s
r = a
i = 0
while (r >= d):
r = r - d
#print ('r = ',r)
i = i+1
#print ('i = ',i)
print (i)
I think it does what I expect it to, but its way too slow to calculate such a large number, I waited 5 mins for i to print (while python used 100% cpu to calculate..), but it didn't. Is there a more efficient way of rewriting this piece of code so I can see how many iterations (i) it takes to complete?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用模和除运算符。
还有一个 divmod 函数可以将两者一起计算:
Use the modulo and division operators.
There is also a divmod function to calculate both together:
您可以使用
i = a/d
。 :DYou can use
i = a/d
. :D您要找的不是一个部门吗?
Isn't a division what you're looking for?
尝试
3833333333.3333333333333333333333
。又名r / d
。try
3833333333.3333333333333333333333
. AKAr / d
.看起来你正在对我做截断除法。也就是说,您想在不知道余数的情况下找出 d 进入 a 的次数。
Looks like you are doing truncating division to me. That is, you want to find out how many times d goes into a without knowing the remainder.