题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
http://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
while True:
try:
a = int(input())
b = []
#质数判断
def prime(n):
s = []
for i in range(1, n//2+1):
if n % i == 0:
s.append(i)
if len(s) != 1:
return False
else:
return True
#找到所有质数对
for i in range(2, a//2+1):
for j in range(i, a):
if i+j == a and prime(i) and prime(j):
b.append([i,j])
d={} #新建字典
#找到质数对的最短距离
for i in range(len(b)):
d[i] = b[i][1]- b[i][0]
mini = min(d.values())
#看哪个质数对达到了最短举例就输出对应的质数对
for i in range(len(b)):
if d[i] == mini:
print(b[i][0])
print(b[i][1])
except:
break