哥德巴赫猜想的另一个形式是:任何大于7的奇数都可以表示成3 个素数之和。例如:15=3+5+7
1.[代码][Java]代码
package cn.ittest.Dome;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class GoldbachConjecture2 {
/**
* 哥德巴赫猜想的另一个形式是:任何大于7的奇数都可以表示成3 个素数之和。例如:15=3+5+7。
* @param args
*/
public static void main(String[] args) {
GoldbachConjecture2 aa = new GoldbachConjecture2();
System.out.print("请输入一个大于7的奇数:");
int a = aa.getNumber();
int[] b = aa.numberResolve(a);
System.out.println(a + "=" + b[0] + "+" + b[1] + "+" + b[2]);
}
// 输入数据
public int getNumber() {
String s = " ";
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
s = in.readLine();
} catch (IOException e) {
}
int a = Integer.parseInt(s);
return a;
}
// 分解这个数,并返回这个数的值
public int[] numberResolve(int a) {
int b[] = { 0, 0, 0 };
for (int i = 1; i < a / 2; i = i + 2)
for (int j = i + 2; j < a / 2; j = j + 2) { //
if (judgeNumber(i) && judgeNumber(j) && judgeNumber(a - i - j)) {
b[0] = i;
b[1] = j;
b[2] = a - i - j;
}
}
return b;
}
// 判断一个数是否为素数(素数就是只能被1和本身整除的数)
public boolean judgeNumber(int a) {
for (int i = 2; i < a; i++) {
if (a % i == 0)
return false;
}
return true;
}
}