‘壹’ 濡备綍鐢↗ava楠岃瘉鍝ュ痉宸磋但鐚沧兂锛
銆銆鍝ュ痉宸磋但鐚沧兂铡熸弿杩版槸钬滀换涓澶т簬5镄勬暣鏁伴兘鍙鍐欐垚涓変釜璐ㄦ暟涔嫔拰钬濓纴娆ф媺缁椤嚭绛変环鐗堟湰锛屽嵆钬滀换涓澶т簬2镄勫伓鏁伴兘鍙鍐欐垚涓や釜璐ㄦ暟涔嫔拰钬濓纴浠婃棩甯歌佺殑闄堣堪涓烘ф媺鐗堟湰銆
銆銆浣跨敤java楠岃瘉鍝ュ痉宸磋但鐚沧兂镄勪唬镰佸备笅锛埚湪浠g爜涓瑕佹眰杈揿叆澶т簬6镄勬暟瀛楁槸锲犱负4=2+2銆6=3+3锛岀畻娉旷洿鎺ヤ粠8寮濮嬮獙璇侊级锛
package algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class GoldbachConjecture {
public static void main(String[] args) {
int i, j, n = 0, f1, f2, m = 0;
System.out.println("楠岃瘉鍝ュ痉宸磋但鐚沧兂銆");
System.out.println("杈揿叆涓涓澶т簬6镄勫伓鏁帮细");
BufferedReader rr = new BufferedReader(new InputStreamReader(System.in));
try {
m = Integer.parseInt(rr.readLine());
} catch (IOException e) {
}
for (i = 3; i < m / 2; i += 2) {
f1 = f2 = 0;
for (j = 3; j <= i / 2; j += 2) {
if (i % j == 0) {
f1 = 1;
break;
}
}
if (f1 == 1)// 濡傛灉i涓嶆槸璐ㄦ暟,涓嬩竴涓猧
continue;
// 鐩村埌镓惧埌涓涓璐ㄦ暟i,杩欐槸镶瀹氩彲浠ユ垒鍒扮殑
n = m - i;
for (j = 3; j <= n / 2; j += 2) {
if (n % j == 0) {
f2 = 1;
break;
}
}
if (f2 == 1)
continue;
else
break;
}
System.out.println("澶т簬绛変簬6镄勫伓鏁" + m + "鍙浠ュ垎瑙d负:");
System.out.println(i + "涓" + n + "涓ゆ暟涔嫔拰");
}
}