"""
@author:Hao
@file:case5py
@time:2022/05/26
"""
def judgePrimeumber(num):
judge = True
if num<=1: # 1和0特判
return False
for i in range(2,int(pow(num,0.5))+1):
if num%i==0:
judge = False # 除了1和自身还可以被其他数整除,非素数
break
return judge
if __name__ == '__main__':
file = open("res.txt", 'w').close()
count = 0 # 计数器
for i in range(2,20220520):
if judgePrimeumber(i): # 该数是素数
strs = str(i)
length = 0
for j in range(len(strs)): # 遍历每一位
if judgePrimeumber(int(strs[j])):
length +=1
if length==len(strs): # 是纯素数
count+=1
with open('res.txt','a') as fp:
fp.write(strs)
fp.write('\n')
with open('res.txt', 'a') as fp:
fp.write('结果{}'.format(count))
【素数算法】求解纯素数
最新推荐文章于 2024-09-04 14:45:59 发布