我正在研究一种在 Java 中创建布尔数组isPrime的方法:
boolean[] isPrime;
其中素数标记为“真”,其余标记为“假”。
虽然我在这里,但我还想计算找到的素数:
int numPrimesFound;
基本思想是使用埃拉托色尼筛。到目前为止,我的方法如下所示:
public Sieve(final int limit) {
long startTime = System.currentTimeMillis();
boolean[] isPrime = new boolean[limit];
this.isPrime = isPrime;
for (int i=3; i<=limit ;i+=2) {
isPrime[i] = true; //sets all even numbers to true
}
isPrime[2] = true;
numPrimesFound = 1; //special case of '2'
for (int i = 3; i * i <= limit; i += 2) {
if (isPrime[i] == true) {
for (int j = i; i * j <= limit; j += 2) {
isPrime[i * j] = false; //has a multiple ==> not a prime
numPrimesFound++; //doesn't work yet
}
}
}
long stopTime = System.currentTimeMillis(); //measuring execution time
System.out.println("Sieve: " + (stopTime - startTime) + " milliseconds.")
}
所以我的问题是
numPrimesFound++:
不起作用,因为筛子不止一次将一些非素数的值设置为“假” (例如 45 bcs 3*15 = 45 和 9*5 = 45)。
那么有没有人知道我如何重写这个程序,以便它只将所有非质数设置为“假”一次?
或者一般来说,任何人都可以提出使该方法更快的方法吗?