开发者

More efficient method for this calculation?

开发者 https://www.devze.com 2022-12-12 05:00 出处:网络
a = 21850开发者_运维技巧0000000 s = 6 f = 2 k = 49 d = k + f + s r = a i = 0 while (r >= d): r = r - d
a = 21850开发者_运维技巧0000000

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


r = (a % d)
i = (a / d)

Use the modulo and division operators.

There is also a divmod function to calculate both together:

i, r = divmod(a,d)


You can use i = a/d. :D


Isn't a division what you're looking for?


try 3833333333.3333333333333333333333. AKA r / d.


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.

a = 218500000000
s = 6
f = 2
k = 49
d = k + f + s

i = a // d

print (i)
0

精彩评论

暂无评论...
验证码 换一张
取 消