简体   繁体   English

为什么在此素数检查中得到ArrayIndexOutOfBoundsException?

[英]Why do I get an ArrayIndexOutOfBoundsException in this prime number check?

I was finding out highest prime factor which divides num, as shown in program, there's a issue with array and 我发现最大的素数除以num,如程序所示,数组和

arr[j] = i;
j++;
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at primenum.main(primenum.java:13)
//to find highest prime factor
public class primenum {
    public static void main(String[] args) {
          double num = 600851475143.0;
           int j = 1;
          int arr[] = {j};

          for(int i=2; i<=num/2; i++)
          {
              if((num%i) == 0 )
              {
                  arr[j] = i;
                  j++;
              }

          }
          // take the last item from array, coz its last big prime
          System.out.println("largest prime is "+ arr[j-1]);

    }
}

What is best way to solve this problem?? 解决此问题的最佳方法是什么?

I'm solving this problem by, 我正在解决这个问题,

  • checking factors until num/2, 检查因子直到num / 2,
  • push all into an array, 全部推入一个数组,
  • check last element...... 检查最后一个元素……

For prime I need to do more, but I'm stuck in initial stage. 首先,我需要做更多的事情,但是我仍然处于起步阶段。

This line 这条线

int arr[] = {j};

Creates an array that only contains the value of j when it is executed. 创建一个仅在执行时包含j值的数组。 You probably want 你可能想要

int arr[] = new int[j];

UPDATE: Based on the answer you left below, trial division is taking too long. 更新:根据您在下面留下的答案,审判部门花费的时间太长。 The Sieve of Eratosthenes is a classic algorithm that is pretty efficient, but the Sieve of Atkin is one of the most advanced algorithms for finding primes. EratosthenesSieve是一种非常有效的经典算法,但是AtkinSieve是查找素数的最先进算法之一。

By creating array arr[] = {j}, you have created an array which contains simply j, or 1. That means the length of the array is 1, because it contains 1 element. 通过创建数组arr [] = {j},您创建了一个仅包含j或1的数组。这意味着该数组的长度为1,因为它包含1个元素。 Thus, arr[1] is out of bounds. 因此,arr [1]超出范围。 Java does not dynamically resize arrays, so you must create a sufficiently large array to contain all of the data you plan to hold. Java不会动态调整数组的大小,因此您必须创建足够大的数组以包含计划保留的所有数据。 Either that or use something like an ArrayList, which is dynamically resizeable. 要么使用动态可调整大小的ArrayList之类的东西。

It looks like you are finding all divisors of num ; 看来您正在找到num所有除数; one of these will be the largest prime factor. 其中之一将是最大的主要因素。 Two related facts alone should help make the problem tractable for smallish numbers: 仅两个相关事实就可以使问题对于较小的数字变得易于处理:
1. If d is a divisor, then so is num/d . 1.如果d是除数,那么num/d也是如此。
2. you needn't check for any divisors greater than the sqrt(num) . 2.您不需要检查任何大于sqrt(num)除数。

To keep track of divisors, use a Set object. 要跟踪除数,请使用Set对象。

Arrays in java are not lists: once allocated, your array won't grow magically. Java中的数组不是列表:分配后,您的数组就不会神奇地增长。

You created the array with: int arr[] = {j}; 您使用以下方法创建了数组: int arr[] = {j}; thus the array has one cell only. 因此该阵列只有一个单元。

You should initialise your array with at least num/2 cells, with something like int arr[] = new int[num/2]; arr[0] = j; 您应该使用至少num/2单元格初始化数组,例如int arr[] = new int[num/2]; arr[0] = j; int arr[] = new int[num/2]; arr[0] = j;

Looks like you start j = 1 and your array only has one element in it to begin, so on the first pass through your for loop you look for arr[1], but the first element in an array is at arr[0]. 看起来您开始j = 1,并且数组中只有一个元素开始,因此在您的for循环的第一遍中,您寻找arr [1],但是数组中的第一个元素位于arr [0]。 Java arrays are zero indexed meaning if you have 10 elements in the array they are located in arr[0] to arr[9]. Java数组是零索引的,这意味着如果数组中有10个元素,它们位于arr [0]至arr [9]中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM