我需要得到一个大数范围内n个连续数字的最大和。在
例如,范围可以是5^150000,在这个范围内,我想找出50000个连续数字的最大和。在
我使用两个循环的方法似乎永远不会终止。我将感谢您的任何意见。在
代码:count = 0
tempsum = 0
summ = 0 # variables init
oldlist = ''
newlist = ''
num = str(3**2209) # for example
for digit in num: # go over all the digits in the number
while count < 12 and len(num) >= 12 : # check in 12-digits blocks while possible
oldlist += num[count] # update old list with new digit
tempsum += int(num[count]) # add current digit to sum
count += 1
if tempsum > summ: # check if new sum is larger than old one
summ = tempsum # update sum
newlist = oldlist # update the sequence list
oldlist = ''
count = 0
tempsum = 0
num = num[1:] # slice the 'first' digit of the number
print(newlist, summ) # print sequence and desired sum