用python编写一个程序验证哥德巴赫猜想

2025-04-07 17:16:23
推荐回答(1个)
回答1:

a = []
f = {}
# 遍历数字
for i in range(2,10001):
    for x in range(2,10001):
        if i%x == 0:
            f[i] = f.get(i, 0) + 1

# 取出所有质数
for i in f:
    if int(f[i]) == 1:
        a.append(i)
    else:
        pass
txt = open('guess.txt', 'w')

# 将所有的质数的算法写入文件
for i in a:
    for x in a:
        l = i + x
        if l%2 == 0 and l < 10000 and i >= x:
            txt.write("%s = %s + %s\n" %(l, i, x))

txt.close()