本例要求根据用户输入的偶数找出其素数和的分解形式
i = int(input("请输入大于2的偶数:"))
value = 0
l = []
if i >2 and i%2 ==0:
for j in range(2,i+1):
for k in range(2,j):
if j%k == 0:
break
else:
l.append(j)
for k in range(len(l)):
for f in range(len(l)):
value = l[k] +l[f]
if i == value:
if l[k] <= l[f]: #为了去除重复,例如:8=3+5 8=5+3
print(i,"=",l[k],"+",l[f])
else:
print("输入的数值不符合要求")
#
# 刘:
def main():
# 输入待验证的偶数
N = int(input("请输入待验证的偶数:"))
while N < 3 or N % 2 == 1:
print("输入的数不符合要求")
N = int(input("请输入待验证的偶数n(n>2):"))
# 生成素数表
Prime = set()
for i in range(2, N + 1):
Prime.add(i)
for i in range(2, N + 1):
if i in Prime:
for k in range(2 * i, N + 1, i):
if k in Prime:
Prime.remove(k)
# 验证该偶数能否分解为两个素数之和
for e in Prime:
f = N - e
if f >= e and f in Prime:
print(N, '=', e, '+', f)
main()