标签:开平 == 素数 循环 and 等于 网上 continue 个数
#求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)
标签:开平 == 素数 循环 and 等于 网上 continue 个数
原文地址:http://blog.51cto.com/6300167/2345467