我为Python素数生成器编写了代码,以生成前100个素数.但是,不知何故,我在输出中得到22,25等非素数.我现在一遍又一遍地重新检查了几个小时,但仍然无法弄清楚我哪里出错了......请帮忙!
这是我的代码:
from math import sqrt
y=[2]
x=3
while len(y)!=100:
for i in range (2,int(round(sqrt(x)+1))):
if x%i==0:
x=x+1
else:
y.append(x)
x=x+1
break
print(y)
Run Code Online (Sandbox Code Playgroud) 我想知道如何在python中找到1s和0s字符串中最长重复1的长度,同时考虑到空字符串.喜欢'1011110111111'会回来6然后''回来0.
过去的帖子建议使用正则表达式,
max([len(i) for i in re.compile('(1+1)').findall(count)])
但它不计算单个1和空字符串的情况.