简体   繁体   English

为什么我的枚举数字列表的主要因素的程序会返回意外的值?

[英]Why does my program for enumerating the prime factors of a list of numbers return unexpected values?

Given a list of integers from a minimum to a maximum, I am trying to find the list of prime factors that evenly multiply into any of the integers in the previously given list.给定一个从最小值到最大值的整数列表,我试图找到与先前给定列表中的任何整数均匀相乘的素数列表。

For example, given as input list of integers [1... 10] , the program would output [1, 2, 2, 2, 3, 3, 5, 7] .例如,作为整数的输入列表[1... 10] ,程序将 output [1, 2, 2, 2, 3, 3, 5, 7] This list contains the prime factors for the list [1... 10] , because 1*1=1 , 1*2=2 , 1*3=3 , 2*2=4 , 5*1=5 , 2*3=6 , 1*7=7 , 2*2*2=8 , 3*3=9 , & 2*5=10 .此列表包含列表[1... 10]的主要因子,因为1*1=11*2=21*3=32*2=45*1=52*3=61*7=72*2*2=83*3=92*5=10

Anyway, in order to do this, I created a method (in Java) to enumerate all the integers from a min to a max.无论如何,为了做到这一点,我创建了一个方法(在 Java 中)来枚举从最小值到最大值的所有整数。 From there, I built a method to find the prime factors of any given integer.从那里,我建立了一种方法来找到任何给定 integer 的主要因素。 Using that prime factors method, I then wrote a method to iterate through a list of integers, keeping a running list of the prime factors needed for these integers without adding any extras that aren't already in this list.使用该素因子方法,我然后编写了一个方法来遍历整数列表,保留这些整数所需的素因子的运行列表,而不添加任何不在此列表中的额外内容。 For example, if the prime factors for only 2 & 8 are needed, it would find the prime factors of 8 (2, 2, & 2), and the prime factors of 4 (2 & 2).例如,如果只需要 2 和 8 的质因数,它将找到 8 的质因数(2、2 和 2)和 4 的质因数(2 和 2)。 However, because 2 & 2 are already in the list because of 8's prime factors, they won't be added.但是,由于 2 和 2 已经在列表中,因为 8 的主要因素,它们不会被添加。 Of course, there are other helper methods in the program that may very well have bugs, but this is the main structure of it.当然,程序中还有其他的辅助方法很可能有bug,但这是它的主要结构。

I tested this program on the list [1... 10] and expected the output [2, 2, 2, 3, 3, 5, 7] , but instead got the output [3, 3, 7] .我在列表[1... 10]上测试了这个程序,并期望 output [2, 2, 2, 3, 3, 5, 7] ,但得到了 output [3, 3, 7] It appears as if, for some reason, when I take the relative complement of the prime factors for a number in the integer list and the running list, some of the numbers in the running list are removed, but I can't figure out why.似乎由于某种原因,当我对 integer 列表和运行列表中的数字进行质因数的相对补码时,运行列表中的一些数字被删除了,但我不知道为什么.

Can anyone find this bug and tell me why this is happening?谁能找到这个错误并告诉我为什么会这样?

Code:代码:

import java.io.*; 
import java.util.*; 
import java.lang.Math;

class Main{

  public static void main(String[] args){

    System.out.println(listPrimeFactors(maxToMin(1, 10)));

  }

  public static ArrayList<Integer> listPrimeFactors(ArrayList<Integer> myList){

    ArrayList<Integer> myFactors = new ArrayList<Integer>();

    for(int j = 0; j < myList.size(); j++){

      myFactors =  addDifference(myFactors, primeFactors(myList.get(j)));

    }
    return myFactors;
  }

  private static ArrayList<Integer> addDifference(ArrayList<Integer> list1, ArrayList<Integer> list2){

    ArrayList<Integer> tempList1 = list1;
    Collections.sort(tempList1);
    Collections.sort(list2);
    for(int i = 0; i < list2.size(); i++){
      if(inList(list2.get(i), tempList1)){
        tempList1.remove(list2.get(i));
        list2.remove(list2.get(i));
      }
    }

    for(int r = 0; r < list2.size(); r++){
      tempList1.add(list2.get(r));
    }
    System.out.println(list2 + " >>> " + tempList1);

    return tempList1;
  }

    private static boolean inList(int n, ArrayList<Integer>  myList){

      for(int k = 0; k < myList.size(); k++){

        if(n == myList.get(k)){ 
          return true;
        } 
      }
      return false;

    }

  public static ArrayList<Integer> primeFactors(int n){ 

    ArrayList<Integer> primeFactorsList = new  ArrayList<Integer>();
    while (n%2==0){ 
      primeFactorsList.add(2); 
      n /= 2; 
    } 
    for (int i = 3; i <= Math.sqrt(n); i+= 2){ 
       while (n%i == 0) { 
         primeFactorsList.add(i); 
         n /= i; 
       } 
   }
    if (n > 2){
      primeFactorsList.add(n);      
    }   
   return primeFactorsList; 
  }

  public static ArrayList<Integer> maxToMin(int min, int max){

    ArrayList<Integer> myList = new ArrayList<Integer>();
    for(int i = max; i >= min; i--){

      myList.add(i);
    }
    return myList;
  }
}

You should not remove items from a list while looping over it without an iterator, see this question .在没有迭代器的情况下循环遍历列表时,不应从列表中删除项目,请参阅此问题 Furthermore you are continuing to work with your temporary list (where elements have been removed already) instead your running list.此外,您将继续使用您的临时列表(其中元素已被删除)而不是您的运行列表。

Here is an example of those changes:以下是这些更改的示例:

private static ArrayList<Integer> addDifference(ArrayList<Integer> list1, ArrayList<Integer> list2){
    ArrayList<Integer> tempList1 = new ArrayList<>(list1);
    for (Iterator<Integer> iterator = list2.iterator(); iterator.hasNext();) {
        Integer value = iterator.next();
        if (inList(value, tempList1)) {
            iterator.remove();
            tempList1.remove(value);
        }
    }

    for (int r = 0; r < list2.size(); r++){
        list1.add(list2.get(r));
    }

    System.out.println(list2 + " >>> " + list1);

    return list1;
}

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

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