Java经典编程题50道之四十三
一个偶数总能表示为两个素数之和。
public class Example43 {
public static void main(String[] args) {
f();
}
public static boolean fun(int a) {
boolean flag = false;
if (a == 3) {
flag = true;
return (flag);
}
for (int i = 2; i <= Math.sqrt(a); i++) {
if (a % i == 0) {
flag = false;
break;
} else
flag = true;
}
return (flag);
}
public static void f() {
Scanner s = new Scanner(System.in);
int n, i;
do {
System.out.print("请输入一个大于等于6的偶数:");
n = s.nextInt();
} while (n < 6 || n % 2 != 0);
for (i = 2; i <= n / 2; i++) {
if (fun(i) && fun(n - i)) {
int j = n - i;
System.out.println(n + " = " + i + " + " + j);
}
}
}
}