【王道JAVA】【程序 27 求素数】

题目:求 100 之内的素数.

public class WangDao {
	public static void main(String[] args){
		int n = 100;
		printPrime(n);
	}
	
	public static void printPrime(int n) {
		int count = 0;
		
		for (int i = 2; i <= n; i++) {
			boolean flag = true;
			for (int j = 2; j <= Math.sqrt(i); j++) {
				if (i % j == 0) {
					flag = false;
				}
			}
			if (flag) {
				if (count++ % 5 == 0) {
					System.out.println();
				}
				System.out.print(i + "  ");
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/YelloJesse/article/details/89414906