#计算速度,10000以上,相比直接取余数慢
import math
total = 0
#求 1 --- 10000 的质数
for calculating in range(1,10000):
#使用Python内置数学库
root = math.isqrt(calculating)
count_down = root
count_up = root + 1
if calculating != root * root:
#已循环完毕,并找到质数,符合要求,退出while循环
while count_down != 1:
comparison = count_down * count_up
if comparison > calculating:
count_down = count_down - 1
elif comparison < calculating:
count_up = count_up + 1
else:
#已求出两个相乘的数,不符合要求,然后退出while循环
break
#二次判断,是否符合要求
if count_down == 1:
#计算总数
total = total + 1
#打印质数
print(calculating)
else:
#开平方根,刚好是整数,不符合要求,跳过此次for循环
continue
#打印总计,多少个质数
print(total)
Python 计算有限范围内质数的分布
最新推荐文章于 2024-11-15 10:17:44 发布