埃拉托斯特尼筛法 ArrayIndexOutOfBounds
尝试实现一个简单的erathosthenes筛来解决euler项目上的这个问题:
10 以下的素数之和为 2 + 3 + 5 + 7 = 17。
求 200 万以下所有素数的和。
但是,我的代码不断返回此错误:
线程“main”中出现异常java.lang.ArrayIndexOutOfBoundsException: -2147479015 在 Prime.main(Prime.java:28)
谁能给我任何关于为什么的提示?这是代码:
import java.math.BigInteger;
public class Prime {
/*
* Input: an integer n > 1
*
* Let A be an array of bool values, indexed by integers 2 to n, initially
* all set to true.
*
* for i = 2, 3, 4, ..., while i^2 ≤ n: if A[i] is true: for j = i^2, i^2 +
* i, i^2 + 2i, ..., while j ≤ n: A[j] = false
*
* Now all i such that A[i] is true are prime.
*/
import java.math.BigInteger;
public class Prime {
/*
* Input: an integer n > 1
*
* Let A be an array of bool values, indexed by integers 2 to n, initially
* all set to true.
*
* for i = 2, 3, 4, ..., while i^2 ≤ n: if A[i] is true: for j = i^2, i^2 +
* i, i^2 + 2i, ..., while j ≤ n: A[j] = false
*
* Now all i such that A[i] is true are prime.
*/
public static void main(String[] args) {
boolean[] array = new boolean[2000000];
BigInteger counter = new BigInteger("0");
for (int value = 0; value < array.length; value++) {
array[value] = true;
}
for (int i = 2; i < array.length; i++) {
if (array[i]) {
int j = i * i;
while (j > 0 && j < array.length) {
array[j] = false;
j += i;
}
}
}
for (int i = 2; i < array.length; i++) {
if (array[i]) {
counter = counter.add(BigInteger.valueOf(i));
}
}
for (int value = 2; value < array.length; value++) {
if(array[value]){
System.out.println(value + ", ");
}
}
System.out.println("\n" + counter);
}
}
trying to implement a simple sieve of erathosthenes to solve this question on project euler :
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
My code keeps returning this error however :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
-2147479015 at Prime.main(Prime.java:28)
Can anyone give me any hints as to why? Here is the code:
import java.math.BigInteger;
public class Prime {
/*
* Input: an integer n > 1
*
* Let A be an array of bool values, indexed by integers 2 to n, initially
* all set to true.
*
* for i = 2, 3, 4, ..., while i^2 ≤ n: if A[i] is true: for j = i^2, i^2 +
* i, i^2 + 2i, ..., while j ≤ n: A[j] = false
*
* Now all i such that A[i] is true are prime.
*/
import java.math.BigInteger;
public class Prime {
/*
* Input: an integer n > 1
*
* Let A be an array of bool values, indexed by integers 2 to n, initially
* all set to true.
*
* for i = 2, 3, 4, ..., while i^2 ≤ n: if A[i] is true: for j = i^2, i^2 +
* i, i^2 + 2i, ..., while j ≤ n: A[j] = false
*
* Now all i such that A[i] is true are prime.
*/
public static void main(String[] args) {
boolean[] array = new boolean[2000000];
BigInteger counter = new BigInteger("0");
for (int value = 0; value < array.length; value++) {
array[value] = true;
}
for (int i = 2; i < array.length; i++) {
if (array[i]) {
int j = i * i;
while (j > 0 && j < array.length) {
array[j] = false;
j += i;
}
}
}
for (int i = 2; i < array.length; i++) {
if (array[i]) {
counter = counter.add(BigInteger.valueOf(i));
}
}
for (int value = 2; value < array.length; value++) {
if(array[value]){
System.out.println(value + ", ");
}
}
System.out.println("\n" + counter);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题来自于这些行:
发生的情况是,有时
i * i
太大,以至于它绕过拐角(溢出)并变为负值。 Java 没有“检查”整数数学。要解决此问题,您需要将 while 条件更改为以下while(j > 0 && j < array.length)
另外,您的数组大小为 200,000,而不是2,000,000。
The problem is coming from these lines:
What's happening is that sometimes
i * i
is so big that it rounds the corner (overflows) and becomes negative. Java does not have 'checked' integer math. To fix this, you'll want to change your while condition to the followingwhile(j > 0 && j < array.length)
Also, your array is of size 200,000 and not 2,000,000.
不是尖酸刻薄,而是因为你超出了数组的范围。
i
的限制是数组的长度,然后j
至少等于i
的平方。然后,j
被用作第 28 行要访问的数组的位置,这是越界的。Not to be snarky, but it's because you're going out of the bounds of the array. Your limit for
i
is the length of the array, and thenj
is equalling at a minimum the square ofi
.j
is then being used as the location of the array to be accessed at line 28, which is out of bounds.