『壹』 濡備綍鐢↗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 + "涓ゆ暟涔嬪拰");
}
}