import java.util.*;
public class Main3 {
public static int n;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int[] temp = new int[n + 1];
int c = 0;
// 筛选法求素数
for (int i = 2; i <= n; i++)
for (int j = i * 2; j <= n; j += i) {
temp[j] = 1; //使用1标记不是素数
}
for (int i = 2; i <= n; i++) {
if (temp[i] == 0)
System.out.print(i + " ");
}
System.out.println();
}
}
筛选法求素数
最新推荐文章于 2023-01-09 09:37:10 发布