题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37
# 求出所有素数 n = int(input()) su = [] for i in range(2, n): for j in range(2, i//2+1): if i % j == 0: break else: su.append(i) su = sorted(su * 2) # 考虑到类似4=2+2,10=5+5的情况 doubles = [] for i in range(len(su)-1): for j in range(i+1, len(su)): if su[i] + su[j] == n: doubles.append((su[i], su[j])) diff = [] for i in range(len(doubles)): x = abs(doubles[i][0] - doubles[i][1]) diff.append(x) Min = diff.index(min(diff)) min_double = doubles[Min] result = sorted(list(min_double)) for i in result: print(i)