【习题44】

作者: Xplorist | 来源:发表于2017-04-20 13:21 被阅读17次

【程序44】
题目:一个偶数总能表示为两个素数之和。

package com.brx.eg_41_50;

public class Test44 {
    public static void main(String[] args) {
        test();
    }
    public static void test(){
        int i=0;
        boolean flag=true;
        while(i<100){
            i++;
            int n=i*2;
            for(int j=1;j<n;j+=2){
                if(test1(j)){
                    if(test1(n-j)){
                        System.out.println(n+"="+j+"+"+(n-j));
                        flag=false;
                    }
                }
            }
            if(flag){
                break;
            }
        }
    }
    public static boolean test1(int n){
        for(int i=2;i<n;i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
}

相关文章

网友评论

    本文标题:【习题44】

    本文链接:https://www.haomeiwen.com/subject/xtclzttx.html