使用布尔值在 Java 中实施素筛
Implementing a prime sieve in Java using booleans
我正在尝试在 Java 中实施素数筛法,以便我可以计算出小于某个最大值的所有素数之和。我尝试使用一种方法 PrimeSieve 并使用一个布尔数组,如果数字是复合的,我会通过取一个素数并考虑所有小于最大值的整数倍来实现它。
但我在尝试 运行 程序时不断遇到编译器错误,我无法弄清楚问题出在哪里:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert ". class" to complete Expression
The type of the expression must be an array type but it resolved to Class
Syntax error on token "i", delete this token
Syntax error, insert ". class" to complete Expression
The type of the expression must be an array type but it resolved to Class
at Problem3.PrimeSieve(Problem3.java:11)
at Problem3.main(Problem3.java:26)
public class Problem3 {
public static boolean[] PrimeSieve(int max) {
// automatically all entries are false
boolean[] isPrime = new boolean[max];
// when a number isn't prime make the entry true
for (int i = 0; i < max; i++) {
if (!boolean[i]) {
for (int j = 2i; j < max; j += i) {
boolean[j] = true;
}
} else {}
}
// return the isPrime boolean with all the primes as false
return isPrime;
}
public static void main(String[] args) {
boolean[] Primes = new boolean[100];
Primes = PrimeSieve(100);
int i = 0;
int ans = 0;
while (i < 100) {
if (!Primes[i]) {
ans += i;
i++;
} else {
i++;
}
}
System.out.println(ans);
}
}
如能帮助解决这些错误,我们将不胜感激
您遇到了 2 个问题:
1) 您使用 type 的名称而不是 array.
的名称
所以boolean[i]
应该改成isPrime[i]
.
2) Java 不理解 2i
为 "two times i"。你需要写 2*i
.
这将使您的代码编译:
for (int i = 0; i < max; i++) {
if (!isPrime[i]) {
for (int j = 2*i; j < max; j += i) {
isPrime[j] = true;
}
} else {} // By the way - This is really not necessary
}
我正在尝试在 Java 中实施素数筛法,以便我可以计算出小于某个最大值的所有素数之和。我尝试使用一种方法 PrimeSieve 并使用一个布尔数组,如果数字是复合的,我会通过取一个素数并考虑所有小于最大值的整数倍来实现它。
但我在尝试 运行 程序时不断遇到编译器错误,我无法弄清楚问题出在哪里:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error, insert ". class" to complete Expression The type of the expression must be an array type but it resolved to Class Syntax error on token "i", delete this token Syntax error, insert ". class" to complete Expression The type of the expression must be an array type but it resolved to Class
at Problem3.PrimeSieve(Problem3.java:11)
at Problem3.main(Problem3.java:26)
public class Problem3 {
public static boolean[] PrimeSieve(int max) {
// automatically all entries are false
boolean[] isPrime = new boolean[max];
// when a number isn't prime make the entry true
for (int i = 0; i < max; i++) {
if (!boolean[i]) {
for (int j = 2i; j < max; j += i) {
boolean[j] = true;
}
} else {}
}
// return the isPrime boolean with all the primes as false
return isPrime;
}
public static void main(String[] args) {
boolean[] Primes = new boolean[100];
Primes = PrimeSieve(100);
int i = 0;
int ans = 0;
while (i < 100) {
if (!Primes[i]) {
ans += i;
i++;
} else {
i++;
}
}
System.out.println(ans);
}
}
如能帮助解决这些错误,我们将不胜感激
您遇到了 2 个问题:
1) 您使用 type 的名称而不是 array.
的名称
所以boolean[i]
应该改成isPrime[i]
.
2) Java 不理解 2i
为 "two times i"。你需要写 2*i
.
这将使您的代码编译:
for (int i = 0; i < max; i++) {
if (!isPrime[i]) {
for (int j = 2*i; j < max; j += i) {
isPrime[j] = true;
}
} else {} // By the way - This is really not necessary
}