Goldbach conjecture: any even number greater than six can be split into two prime numbers and print out all the possible
inputs for all boundary even number n, n may be output
as input
100
Output:
100 = 3 + 97
100 + 11 = 89
100 = +83 17
100 = 29 + 71
100 = +59 41
100 = 47 + 53
package chapter04;
import java.util.Scanner;
public class test01 {
//将偶数拆分成俩质数的和
public static void main(String[] args) {
int num=inPut();
outPut(num);
}
public static int inPut(){
//输入数字并通过检验确定一个符合要求的数
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
if(num%2!=0||num<=6){
System.out.println("输入错误,请重新输入大于6的偶数:");
return inPut();//错误则返回inPut()继续输入
}
return num;//正确则返回num值
}
public static boolean isPrim(int num){
//判断是否是质数
for(int i=2;i<=Math.sqrt((double)num);i++){
if(num%i==0){
return false;
}
}
return true;
}
public static void outPut(int num){
//输出所有符合条件的质数对
for(int i=2;i<=num/2;i++){
if(isPrim(i)==true&&isPrim(num-i)==true){
System.out.println(num+"="+i+"+"+(num-i));
}
}
}
}