62,623
社区成员
发帖
与我相关
我的任务
分享
public class Prime {
private final static int n = Integer.MAX_VALUE/100;
private final static boolean[] pt = new boolean[n];
public Prime(){
pt[0] = true;
pt[1] = true;
for(int i = 2; i*i < n; i++){
if ( !pt[i]) {
for (int j = i; i*j < n; j++){
if (!pt[i*j]){
pt[i*j] = true;
}
}
}
}
}
public void showPrimes(int n){ //输出 [1,n]中所有的素数
for(int i = 2; i <= n; i++){
if(!pt[i]){
System.out.print(i+" ");
}
}
System.out.println();
}
public static void main(String[] args){
Prime p = new Prime();
p.showPrimes(100);
}
}