任意一个偶数(大于2)都可以由2个素数组成,组成偶数的2个素数有很多种情况,本题目要求输出组成指定偶数的两个素数差值最小的素数对。
数据范围:输入的数据满足
import math def check(num): if num == 1: return False elif num == 2: return True else: for i in range(2,int(math.sqrt(num))+1): if num%i == 0: return False return True while True: try: n = int(input()) l = [-1]*3 for i in range(2,n//2+1): if check(i) and check(n-i): if l[2] < 0: l[0] = i l[1] = n-i l[2] = n - 2*i elif l[2] >= 0 and (n-2*i) < l[2]: l[0] = i l[1] = n-i l[2] = n - 2*i print(l[0]) print(l[1]) except: break
import math def is_prime(num: int) -> bool: if num < 2: return False elif num == 2: return True else: for i in range(2, math.ceil(num**0.5 + 1)): if num % i == 0: return False return True n = int(input()) min_pair = [0,n] min_value = n for i in range(2, int(n / 2)+1): if is_prime(i) and is_prime(n - i): if n - i - i < min_value: min_value = n - i - i min_pair = [i, n - i] print(min_pair[0]) print(min_pair[1])
import math a=int(input()) c=[] d={} for i in range(2,a+1): b=[] for j in range(1,int(math.sqrt(i))+1): if i%j==0: b.append(i//j) b.append(j) if j==int(math.sqrt(i)) and len(b)==2: c.append(i) for k in range(0,len(c)): for l in range(k,len(c)): if c[k]+c[l]==a: d[(k,l)]=c[l]-c[k] e=sorted(d.keys(),key=lambda x : x[1]) print(c[e[0][0]]) print(c[e[0][1]])
import math def isPrime(num): if num == 2: return True for i in range(2, int(math.sqrt(num))+1): if num % i == 0: return False return True while True: try: n = int(input()) # 从中位数到2递减,第一次发现两个素数相加等于输入即为所求 for i in range(n//2, 2, -1): # i不是素数,继续下一次循环 if not isPrime(i): continue # i是素数 else: # 判断n-i是否为素数,如果是,打印 if isPrime(n-i): print(i) print(n-i) break except: break
def checknum(num): if num == 1: return False if num == 2: return True list0 = [] for i in range(2,num): if num % i == 0: list0.append(i) if not list0: return True else: return False while True: try: n = int(input()) num = n // 2 for i in range(num): a,b = num -i,num+i if checknum(a) and checknum(b): print(a) print(b) break except: break
def isPrime(number): if number == 2: return True limited = int(number**0.5+2) for i in range(2, limited): if number % i == 0: return False return True while True: try: number = int(input()) output_1, output_2 = int(number/2), int(number/2) while True: sign_1 = isPrime(output_1) sign_2 = isPrime(output_2) if sign_1*sign_2==1: break output_1 -= 1 output_2 += 1 print(output_1) print(output_2) except: break
def f(x): # 写一个函数,用来判断素数 if x <= 1:return False # 1既不是素数,也不是合数 elif x == 2:return True #2是素数 #return之后的代码不会执行 else: for i in range(2,int(x**0.5)+1): #节约时间 if x%i == 0:return False #不是素数 else:return True #是素数 n = int(input()) # n是偶数 for i in range(int(n/2),1,-1): #n/2是偶数,从 n/2-1 开始遍历,找第1对即可 if f(i) and f(n-i): print(i) # i越来越小,所以i一定小于n-1 print(n-i);break
def su(num): #判断该数是否为素数 a = [] for i in range(1,num+1): if num%i == 0: a.append(i) if len(a) == 2: return 1 else: return -1 while True: try: n = int(input()) a = [] for i in range(2,n): #从2开始,有序递增,产生的素数也是从小到大排列好的 num = i if su(num) == 1: #该数为素数时 a.append(i) #所有素数放入列表a b = [] for i in range(len(a)): if (n-a[i]) in a: #筛选出两数只和为n的情况 if a[i] > n//2: #去除后半段重复的数据 break b.append(a[i]) #满足条件的数据放入新的列表,因为数据为有序数据,最后一组则为最佳答案 print(b[len(b)-1]) print(n-b[len(b)-1]) except: break#常规方法,好理解些
def isPrime(n): #判断是否是 素数 if n == 2: return True for i in range(2,int(n**0.5)+1): if n%i == 0: return False return True n = int(input()) # 输入n=20 prime = [] #所有素数 组成的列表 for i in range(2,n//2+1): #取整除 - 返回商的整数部分(向下取整)9//2=4,,,-9//2=-5 if isPrime(i): #是素数 #prime = prime + [i] prime.append(i) prime.sort(reverse=True) # 大小排序 [7, 5, 3, 2] #print(prime) for i in prime: if isPrime(n-i): # 输出两个素数差值最小的素数对 print(i) # 7 print(n-i)# 13 break
#?判断是否是素数 def duan(num): count = 1 for ele in range(2,num+1): if num%ele == 0: count+=1 if count == 2: return True else: return False while True: try: number = int(input()) data = [] n_list = [] n_min =[] except: break else: for ele in range(2,number): temp = number - ele if ele+temp == number: data.append([ele,temp]) #?print(data) for ele in data: k = duan(ele[0]) v = duan(ele[1]) if k == True and v == True: n_list.append(ele) #?print(n_list) for data in n_list: n_min.append(abs((data[0]-data[1]))) #?print(min(n_min)) n_index = n_min.index(min(n_min)) #?print(n_min.index(min(n_min))) for ele in ((sorted(n_list[n_index]))): print(ele)