欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

哥德巴赫猜想,,哥德巴赫猜想 哥德巴赫猜

来源: javaer 分享于  点击 3268 次 点评:135

哥德巴赫猜想,,哥德巴赫猜想 哥德巴赫猜


哥德巴赫猜想 哥德巴赫猜想的一个命题是:任何一个大于2 的偶数都是两个素数之和。例如:4=2+2、10=3+7……。

package cn.ittest.Dome;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class GoldbachConjecture {    /**     * Goldbach Conjecture哥德巴赫猜想 哥德巴赫猜想的一个命题是:任何一个大于2 的偶数都是两个素数之和。例如:4=2+2、     * 10=3+7……。     *      * @param args     */    public static void main(String[] args) {        GoldbachConjecture aa = new GoldbachConjecture();        System.out.print("请输入一个偶数:");        int a = aa.getNumber();        System.out.println(a + "=" + aa.numberResolve(a) + "+"                + (a - aa.numberResolve(a)));    }    // 输入数据    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;        for (int i = 1; i < a / 2; i++)            if (judgeNumber(i) && judgeNumber(a - i))                b = i;        return b;    }    // 判断一个数是否为素数(素数就是只能被1和本身整除的数)    public boolean judgeNumber(int a) {        for (int i = 2; i < a; i++) {            if (a % i == 0)                return false;        }        return true;    }}//该片段来自于http://byrx.net
相关栏目:

用户点评