如何找到 10000 以下最长的合数序列?我如何只打印最长的系列?
How do I find the longest series of composite numbers below 10000? How do I only print the longest serie?
这是我的代码:
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
我希望它只给出最长的系列来打印,我想我需要将变量系列存储在某个地方并检查每次迭代系列的新值是否高于前一个,如果更高则交换, 然后只打印一次。但是我不知道如何有效地存储和交换。
希望这足够清楚
我使用了一个 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)
这是我的代码:
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
我希望它只给出最长的系列来打印,我想我需要将变量系列存储在某个地方并检查每次迭代系列的新值是否高于前一个,如果更高则交换, 然后只打印一次。但是我不知道如何有效地存储和交换。
希望这足够清楚
我使用了一个 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)