任意一个偶数(大于2)都可以由2个素数组成,组成偶数的2个素数有很多种情况,本题目要求输出组成指定偶数的两个素数差值最小的素数对。
数据范围:输入的数据满足
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#常规方法,好理解些
#include<bits/stdc++.h> using namespace std; bool isPrime(int a){ for(int i=2;i<=sqrt(a);i++) if(a%i==0) return false; return true; } int main(){ int a; cin>>a; for(int i=a/2;i>=2;i--){ if(isPrime(i)&&isPrime(a-i)){ cout<<i<<endl<<a-i; return 0; } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = br.readLine()) != null) { int n = Integer.parseInt(s); for (int i = n / 2; i >= 2; i--) { if (prime(i) && prime(n - i)) { System.out.println(i); System.out.println(n - i); break; } } } } public static boolean prime(int n) { for (int i = 2; i < n; i++) { if (n % i == 0) return false; } return true; } public String destCity(List<List<String>> paths) { return ""; } }
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; while ((str = br.readLine()) != null) { int n = Integer.parseInt(str); for (int i = n / 2; i < n - 2; i++) { if (isPrime(i) && isPrime(n - i)) { System.out.println(String.format("%d\n%d", n - i, i)); break; } } } br.close(); } private static boolean isPrime(int n) { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) return false; } return true; } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while((line = br.readLine()) != null) { int target = Integer.parseInt(line.trim()); int small = target / 2; int big = target - small; boolean smallIsPrime = false, bigIsPrime = false; do{ smallIsPrime = isPrime(small); if(!smallIsPrime) { small --; big = target - small; continue; } big = target - small; bigIsPrime = isPrime(big); if(!bigIsPrime) { small --; big = target - small; } }while(small > 2 && !(smallIsPrime && bigIsPrime)); System.out.println(small); System.out.println(big); } } private static boolean isPrime(int num) { for(int factor = 2; factor <= (int)Math.sqrt(num) + 1; factor++) if(num % factor == 0) return false; return true; } }
def is_prime(num): for i in range(2,num-1): if num%i ==0: return False return True while True: try: num = int(input()) left, right = num//2, num//2 while left>=2 and right<=num-1: if left + right > num: left -=1 elif left + right < num: right +=1 else: if not is_prime(left): left-=1 continue if not is_prime(right): right+=1 continue print(left) print(right) break except Exception as e: break
def check_prime(num): for i in range(2, int(num**1/2)+1): if num % i == 0: return False return True while True: try: even = int(input().strip()) half = even // 2 start = 0 if half % 2 == 1 else 1 for i in range(start, half, 2): a, b = half + i, half - i if check_prime(a) and check_prime(b): print(b) print(a) break except: break(1)选择从中间数开始,一加一减,保证和是输入数的同时,还达到了两个素数差值最小。(2)从奇数中寻找素数,每次跨度为2.
#include<bits/stdc++.h> using namespace std; bool is_su(int m) { bool flag=1; for(int i=2;i*i<=m;i++) { if(m%i==0) { flag=0; } } return flag; } int main() { int n; while(cin>>n) { for(int i=n/2;i>=1;i--) { if(is_su(i)==1) { if(is_su(n-i)==1) { cout<<i<<endl; cout<<n-i<<endl; break; } } } } return 0; }
#include "string.h" #include <stdio.h> int sushuo(int a) /*判断一个数是不是素数,是的话返回1,不是返回0*/ { int i=2; for (i=2;i<a;i++) { if (a%i==0) return 0; } return 1; } int main (void) { int input; while (scanf("%d",&input)!=EOF) { int a=0,b=0; a=b=input/2; int i; int j; for (i=0;i<input/2;i++) { if (sushuo(a)==0 || sushuo(b)==0) { a++; b--; } } printf("%d\r\n",b); printf("%d\r\n",a); } }
#include <iostream> #include <cmath> using namespace std; //一个从头出发,一个从尾巴出发,判断两数是否为素数,若是,则记录下来 //头的数不能大于尾巴的数 作为出口 //输出最终记录的值 bool isnum(int n) { bool flag = true; for(int i = 2;i<=sqrt(n);i++) { if(n%i == 0) { flag = false; } } return flag; } int main() { int n; while(cin >> n) { int a = 0,b=0; for(int i =1 ;i<=n;i++) { if(isnum(i) && isnum(n-i) && (i<=(n-i))) { a = i; b = n-i; } } cout<<a<<endl<<b<<endl; } return 0; }
#include <iostream> #include <algorithm> using namespace std; // 判断素数 bool isPrime(int a){ int tmp = sqrt(a);//只遍历到开方出,不能被开方左边的数整除,则一定不能被开方右边的数整除for(int i = 2; i <= tmp; ++i){ if(a % i == 0){ return false; } } return true; } int main(){ int n; while(cin >> n){ int i = n / 2; // 从中间向两边找 for(; i > 0; --i){ if(isPrime(i) && isPrime(n - i)){ break; } } cout << i << endl << n - i << endl; } return 0; }
/** * 思路:由于差值最小的素数对 肯定是最靠近这个数的中位数, * 所以从中位数开始,依次判断是否是素数对,是的话直接输出即可 */ import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { minSushuDui(scanner.nextInt()); } } /** * 找出差值最小的素数对 * @return */ public static void minSushuDui(int n) { for (int i = n / 2; i < n; i++) { if (isSushu(i) && isSushu(n-i)){ System.out.println(n-i); System.out.println(i); break; } } } /** * 判断一个数是否为素数: * 从2开始遍历i++,到数字本身大小之前停止,每次遍历判断这个数字能否被i整除。 * 如果能被i整除,即num%i==0,则不是质数,返回false,否则返回true。 */ public static boolean isSushu(int num) { for (int i = 2; i < num; i++) { if (num%i==0){ return false; } } return true; } }
#include<iostream> using namespace std; int main() { int n; while(cin>>n) { for(int i=n/2;i>0;i--) { int j=2; for(;j<i/2;j++)//判断i是不是质数 { if(i%j==0) break; } if(i/2==j)//当i是质数判断n-i是不是 { int b=n-i;j=2; for(;j<b/2;j++) { if(b%j==0) break; } } if((n-i)/2==j)//两个都是质数 { cout<<i<<endl<<n-i<<endl; break; } } } return 0; }
while True: try: even = int(input()) result = [] res = [] for i in range(2,even): for j in range(2,i): if i % j == 0: break else: result.append(i) for i in result: for j in result[::-1]: if i + j == even: if abs(j-i) not in res: res.append(abs(j-i)) res.sort() for i in result: for j in result[::-1]: if i + j == even and j - i == res[0]: print(i) print(j) except: break
while True: try: a,b,c = int(input()),[],[] for i in range(int(a/2),a): for j in range(2,int(i/2)+1): if i%j == 0: break elif j == int(i/2): b.append(i) for i in b: for j in range(2,int((a-i)/2)+1): if (a-i)%j == 0: break elif j == int((a-i)/2): c.append(i) print(a-c[0]) print(c[0]) except: break
import math def is_prime(n): if n == 2: return True for i in range(2, int(math.sqrt(n)) + 1): if n % i == 0: return False return True while True: try: n = eval(input()) #从中间开始判断 a = n // 2 b = n - a while not is_prime(a)&nbs***bsp;not is_prime(b): a -= 1 b += 1 if a >= b: print("{}\n{}".format(b, a)) else: print("{}\n{}".format(a, b)) except: break