题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
''' while True: try: n=int(input()) prime_n=[] for i in range(2,n): for j in range(2,int(i**0.5)+1): if i%j==0: break # i能被j整除,则不是素数,跳出j循环,进行下一个i循环 else: prime_n.append(i) #print(prime_n) diff=[] for each in prime_n: l=[each,n-each,abs(2*each-n)] column=[row[2] for row in diff] if n-each in prime_n and abs(2*each-n) not in column: diff.append(l) #print(diff) diff_num=[row[2] for row in diff] for i in diff: if i[2]==min(diff_num): print(i[0]) print(i[1]) except: break ''' # 先定义一个素数判别函数,再判断 i 和 n-i 是否均为素数,较为简单 def isSuShu(x): if x <= 2: return True else: for i in range(2, int(x ** 0.5) + 1): if x % i == 0: return False else: return True #print(isSuShu(3),isSuShu(4)) while True: try: n = int(input()) for i in range(int(n / 2), n): #两加数,必定一个在(1,n/2),一个在(n/2+1,n);离n/2最近的俩素数 差最小。 if isSuShu(i) and isSuShu(n - i): print(n - i) print(i) break except: break