#求10万内的所有素数(9592)
print(2)
count = 1
for i in range(3,100000):
for j in range(2,i):
if i%j ==0:
break
if j==i-1:
print(i)
优化:
count = 1
for i in range(3,100000,2):#跳过所有偶数
for j in range(2,i):
if i%j ==0:
break
if j==i-1:
count +=1
print(count)
再优化:
count = 1
for i in range(3,100000,2):
for j in range(2,int(i**0.5)+1):#便利到i的开平方
if i%j ==0:
break
else:
count +=1
print(count)
再再优化:
#由于数学成绩有限,方法来自网上
count = 2 #大于等于5的素数一定和6的倍数相邻,所以2、3不在循环内统计,
n = 100000
for num in range(4,n):
if num%6 != 1 and num%6 !=5:
continue
else:
snum = int(num**0.5+1)
for i in range(5,snum):
if not num%i:
break
else:
count +=1
print(count)
再再再优化:
#求10万内的所有素数
count = 2 #大于等于5的素数一定和6的倍数相邻,所以2、3不在循环内统计,
n = 100000
for num in range(4,n):
if num%6 != 1 and num%6 !=5:
continue
else:
snum = int(num**0.5+1)
for i in range(5,snum,2):
if not num%i:
break
else:
count +=1
print(count)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。