这是我的代码:
series = 0
for counter in range (1, 30):
if counter > 1:
for i in range (2, counter):
if (counter % i) == 0:
series += 1
break
else:
print ("The longest series of composite numbers under 10.000 starts at %d and ends at %d the series is %d long" % ((counter-series), counter, series))
series = 0
我希望它只给出最长的序列,我想我需要将变量序列存储在某个地方,并检查每次迭代,如果序列的新值比先前的值高,然后交换,如果序列的值更高,并且然后只打印一次。但是我不知道如何有效地存储和交换。
希望这很清楚
答案 0 :(得分:0)
我使用了for else循环,它起作用了,这是我的最终代码:
series = 0
b = 0
a = 0
for num in range (1, 10000):
for j in range (2, num):
if (num % j) == 0:
series += 1
break
else:
if series > b:
b = series
a = num
series = 0
else:
series = 0
print ("De langste reeks niet-priemgetallen onder de 10.000 begint op %d en eindigt op %d" % (((a-b), a-1)))
print ("de reeks is %d lang" % b)