判断0-2000之间有多少个素数,并输出这些素数

来源:互联网 发布:php读取图片并输出 编辑:程序博客网 时间:2024/04/28 00:39

直接上代码:

package doc_01.second;/** * 模块说明: 判断0-2000之间有多少个素数,并输出这些素数 *  */public class CountPrime {    private static int count = 0;    public static void main(String[] args) {        // test        CountPrime cp = new CountPrime();        int number = 2000;        StringBuffer chinese = new StringBuffer();        cp.getPrimeNumber(number, chinese);        System.out.println("0-2000之间素数个数为:" + count);        System.out.print(chinese.toString());    }    public void getPrimeNumber(int number, StringBuffer chinese) {        chinese.append("1 ");        count++;        chinese.append("2 ");        count++;        boolean isPrime = true;        for (int i = 3; i < number; i++) {            for (int j = 2; j < i; j++) {                if (i % j == 0) {                    isPrime = false;                    break;                }            }            if (isPrime == true) {                chinese.append(i + " ");                count++;            } else {                isPrime = true;            }        }    }}
0 0
原创粉丝点击