判断101-200之间有多少个素数,并输出所有素数。
public class Example02 {
public static void main(String[] args) {
prime();
}
public static void prime() {
int count = 0;
for (int i = 101; i < 2000; i += 2) {
boolean flag = false;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
flag = false;
break;
} else {
flag = true;
}
}
if (flag) {
count++;
System.out.print(i + " ");
if (count % 10 == 0) {
System.out.println();
}
}
}
System.out.println("\n共有" + count + "个素数。");
}
}
时间: 2024-10-06 02:08:45