欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

统计1到n之间的素数

程序员文章站 2022-03-13 09:52:35
...

package com.program;

public class PrimeNumber { public static void main(String[] args) { print(100, 107); }

public static void print(int startNum, int endNum) {
	int count = 0;
	for (int i = startNum; i <= endNum; i++) {
		boolean b = false;
		for (int j = 2; j <= Math.sqrt(i); j++) {
			if (i % j == 0) {
				b = false;
				break;
			} else {
				b = true;
			}
		}
		if (b == true) {
			count++;
			System.out.println(i);
		}
	}
	System.out.println("prime number is : " + count);
}

}

转载于:https://my.oschina.net/u/435726/blog/212700