Scanner sc = new Scanner(System.in);
Long n = sc.nextLong();
// 储存质数的数组
boolean b[] = new boolean[n.intValue() + (int) Math.floor(Math.sqrt(n.intValue()))];
int len = b.length;
// 把数组元素填充为true
Arrays.fill(b, true);
// 总素数
int total = 0;
for (int i = 2; i <= n; i++) {
if (b[i]) {
// 把素数的倍数划掉, 不在判断i
for (int j = 2 * i; j <= n; j += i) {
b[j] = false;
}
}
}
for (int x = 2; x <= n; x++) {
if (b[x]) {
System.out.println(x + " " + (++total));
}
}
大家都喜欢求素数
最新推荐文章于 2023-01-09 09:37:10 发布