我正在编写这个 java 程序来使用 Eratosthenes 的 Sieve 查找直到 num 的所有素数,但是当我尝试编译时,它说我不能使用 long var 作为数组索引,并且它需要一个 int var它的位置。但我将处理大量数字,所以我不能使用 int。我能做些什么?
import java.util.*;
import java.lang.*;
public class t3{
public static void main(String[] args){
long num = 100;
//declaring list and filling it with numbers
ArrayList<Long> numlist = new ArrayList<Long>();
for(long x=2 ; x<num ; x++){
numlist.add(new Long(x));
}
//sieve or eratosthenes
for(long x=0 ; x<Math.sqrt(num) ; x++){
for(long y=x+1 ; y<numlist.size() ; y++){
if(numlist[y]%numlist[x] == 0){
numlist.remove(y);
}
}
}
//print list
for(Object item : numlist){
System.out.println((Long)item);
}
}
}