执行思路:
1-定义素数判断函数
2-执行循环嵌套
附加3-由于计算量较大,可以添加程序运行时计算运行时间
废话不多说,直接上代码:
#来自抗带的铁铁
def isprime(x): #定义素数判断函数
k=int(pow(x,1/2))
for i in range(2,k+1):
if x%i==0:
return False
else:
continue
return True
import datetime #加入运行时
start=datetime.datetime.now()
count=1 #加入计数器,每行打印6个算式
for num in range(4,1000,2):
for n in range(2,num):
if isprime(n):
for m in range(2,num):
if isprime(m):
if m+n==num:
print("{}={}+{}".format(num,m,n),end=",")
count=count+1
if count==7:
print()
count=1
break
else:
continue
else:
continue
else:
continue
end=datetime.datetime.now()
time=end-start
print("打印结束,用时{}".format(time))
运行结果:
4=2+2,6=3+3,8=5+3,8=3+5,10=7+3,10=5+5,
10=3+7,12=7+5,12=5+7,14=11+3,14=7+7,14=3+11,
16=13+3,16=11+5,16=5+11,16=3+13,18=13+5,18=11+7,
18=7+11,18=5+13,20=17+3,20=13+7,20=7+13,20=3+17,
22=19+3,22=17+5,22=11+11,22=5+17,22=3+19,24=19+5,
24=17+7,24=13+11,24=11+13,24=7+17,24=5+19,26=23+3,
26=19+7,26=13+13,26=7+19,26=3+23,28=23+5,28=17+11,
28=11+17,28=5+23,30=23+7,30