java題目 查詢組成一個偶數最接近的兩個素數
阿新 • • 發佈:2022-03-05
描述
任意一個偶數(大於2)都可以由2個素陣列成,組成偶數的2個素數有很多種情況,本題目要求輸出組成指定偶數的兩個素數差值最小的素數對。 本題含有多組樣例輸入。 資料範圍:輸入的資料滿足4 \le n \le 1000 \4≤n≤1000輸入描述:
輸入一個大於2的偶數
輸出描述:
輸出兩個素數
示例1
輸入:20輸出:
7 13
示例2
輸入:4輸出:
2 2
1 import java.io.InputStreamReader; 2 import java.io.BufferedReader; 3 4 publicclass Main{ 5 public static boolean isZhiShu(int num){ 6 for(int n = 2; n < num;n++){ 7 if(num % n == 0){ 8 return false; 9 } 10 } 11 return true; 12 } 13 public static void main(String[] args) throws Exception{ 14 BufferedReader bf = newBufferedReader(new InputStreamReader(System.in)); 15 String str = ""; 16 while((str = bf.readLine()) != null){ 17 int num = Integer.parseInt(str.trim()); 18 for(int m = num/2;m >= 2; m--){ 19 if(isZhiShu(m) && isZhiShu(num - m)){ 20 System.out.println(m);21 System.out.println(num - m); 22 break; 23 } 24 } 25 } 26 } 27 }