- 编写一个函数验证哥德巴赫的猜想:任何一个充分大的偶数(大于等于6)总可以表示成两个素数之和—–要求:将6-100之间的偶数,都用两个素数之和去表示
def function(num):
lu = []
for i in range(6, num):
if i % 2 == 0:
lu.append(i)
# 得到所有的素数
i = 6
Iu = []
while True:
k = 2
ku = []
while True:
if i % k != 0:
ku.append(k)
# 素数只有1和本身整除,所以这个素数n里面所有不整数的数的数量ku为素数n-1
if len(ku) == i - 1:
Iu.append(i)
k += 1
if k > i:
break
i += 1
if i >= num:
break
# 排序
# for i in range(len(Iu)):
# for j in range(1, len(Iu)):
# if Iu[i] + Iu[j] == 6:
# print("%s + %s = 6" % (Iu[i], Iu[j]))
#
# for i in range(len(Iu)):
# for j in range(1, len(Iu)):
# if Iu[i] + Iu[j] == lu[z] and Iu[i] != Iu[j]:
# print("%s + %s = 20" % (Iu[i], Iu[j]))
dudu = {}
for z in range(len(lu)):
for i in range(len(Iu)):
for j in range(1, len(Iu)):
if Iu[i] + Iu[j] == lu[z] and Iu[i] != Iu[j]:
dudu.setdefault("%s + %s" % (Iu[i], Iu[j]), lu[z])
# print(dudu)
for item in dudu:
print("%s,%s" % (item, dudu[item]))
function(1000)