Before we begin let me say that I already found the solution to the problem, but I am still unable to explain why it solved the problem, so I am here to ask you experts why this java code produces the results I indeed see.
The two pairs of code I am going to show are meant to find all the prime factors of a number (it is not my original code, I took it from Project Euler, and I'm further using it solve a later, harder problem in the project, but that is not relevant).
I am going to post the first code and its corresponding incorrect result, then show the second code, its correct result, and I will lastly point out the only line that is different between the two.
The question I have for you guys, because I can't answer it, is why is the first result coming up this way? Why does the solution of changing that single line of code work?
The only explanation I can give is that for some reason if you initiate an ArrayList by saying ArrayList something = oldArrayList they are both pointing to the same ArrayList... I'm not satisfied with this answer because it doesn't make sense with the java structure I thought I knew.
So here we go:
First Code:
Incorrect Result:
5 : 5
1 : 1
2 : 2
Second Code:
Correct Result:
5 : 1
2 : 1
The only line that changes:
Incorrect Line of Code:
Correct Line of Code:
Your help and knowledge is much appreciated,
-blazed
The two pairs of code I am going to show are meant to find all the prime factors of a number (it is not my original code, I took it from Project Euler, and I'm further using it solve a later, harder problem in the project, but that is not relevant).
I am going to post the first code and its corresponding incorrect result, then show the second code, its correct result, and I will lastly point out the only line that is different between the two.
The question I have for you guys, because I can't answer it, is why is the first result coming up this way? Why does the solution of changing that single line of code work?
The only explanation I can give is that for some reason if you initiate an ArrayList by saying ArrayList something = oldArrayList they are both pointing to the same ArrayList... I'm not satisfied with this answer because it doesn't make sense with the java structure I thought I knew.
So here we go:
First Code:
Code:
public static void main(String[] args) { utils.print(allPrimeFactorsAndTheirPowers(10)); } public static HashMap<Integer, Integer> allPrimeFactorsAndTheirPowers(int input) { ArrayList<Integer> primeFactors = new ArrayList<Integer>((int)Math.sqrt((double)input)); ArrayList<Integer> powers = primeFactors; HashMap<Integer, Integer> factorsAndPowers = new HashMap<Integer,Integer>((int)Math.sqrt((double)input)); int currentPower = 0; if (!isPrime(input)) { int n = input; if(n % 2 == 0) { currentPower++; primeFactors.add(Integer.valueOf(2)); n = n / 2; while(n % 2 == 0) { currentPower++; n = n / 2; } powers.add(Integer.valueOf(currentPower)); currentPower=0; } int factor = 3; int maxFactor = (int)Math.sqrt((double) n); while(n > 1 && factor <= maxFactor) { if (n % factor == 0) { currentPower++; n = n / factor; primeFactors.add(factor); while(n % factor == 0) { currentPower++; n = n / factor; } maxFactor = (int)Math.sqrt((double)n); powers.add(Integer.valueOf(currentPower)); currentPower=0; } factor = factor+2; } if (n != 1) { primeFactors.add(Integer.valueOf(n)); powers.add(Integer.valueOf(1)); } } else { primeFactors.add(Integer.valueOf(input)); powers.add(Integer.valueOf(1)); } for(int i = 0; i < primeFactors.size(); i++) { factorsAndPowers.put(primeFactors.get(i), powers.get(i)); } return factorsAndPowers; }
5 : 5
1 : 1
2 : 2
Second Code:
Code:
public static void main(String[] args) { utils.print(allPrimeFactorsAndTheirPowers(10)); } public static HashMap<Integer, Integer> allPrimeFactorsAndTheirPowers(int input) { ArrayList<Integer> primeFactors = new ArrayList<Integer>((int)Math.sqrt((double)input)); ArrayList<Integer> powers = new ArrayList<Integer>((int)Math.sqrt((double)input)); HashMap<Integer, Integer> factorsAndPowers = new HashMap<Integer,Integer>((int)Math.sqrt((double)input)); int currentPower = 0; if (!isPrime(input)) { int n = input; if(n % 2 == 0) { currentPower++; primeFactors.add(Integer.valueOf(2)); n = n / 2; while(n % 2 == 0) { currentPower++; n = n / 2; } powers.add(Integer.valueOf(currentPower)); currentPower=0; } int factor = 3; int maxFactor = (int)Math.sqrt((double) n); while(n > 1 && factor <= maxFactor) { if (n % factor == 0) { currentPower++; n = n / factor; primeFactors.add(factor); while(n % factor == 0) { currentPower++; n = n / factor; } maxFactor = (int)Math.sqrt((double)n); powers.add(Integer.valueOf(currentPower)); currentPower=0; } factor = factor+2; } if (n != 1) { primeFactors.add(Integer.valueOf(n)); powers.add(Integer.valueOf(1)); } } else { primeFactors.add(Integer.valueOf(input)); powers.add(Integer.valueOf(1)); } for(int i = 0; i < primeFactors.size(); i++) { factorsAndPowers.put(primeFactors.get(i), powers.get(i)); } return factorsAndPowers; }
5 : 1
2 : 1
The only line that changes:
Incorrect Line of Code:
Code:
ArrayList<Integer> powers = primeFactors;
Code:
ArrayList<Integer> powers = new ArrayList<Integer>((int)Math.sqrt((double)input));
-blazed
Comment