import java.util.Scanner;
public class test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int num = in.nextInt();
int count = 0;
for (int i = 1; i <= num; i++) {
// 默认是素数
boolean flag = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
// 能整除
flag = false;
}
}
if (flag) {
count += 1;
System.out.print(i + ",");
}
}
System.out.print("在"+num+"以内的数中共计有" + count + "个素数");
}
}
}
执行结果如下:
100
1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,在100以内的数中共计有26个素数