This calculates a chain of numbers.
biggestList and upperBound are initialized on top.
Even if I set i to some non 1 number, it only prints 1-> That's 1 numbers long!
The code was working fine with long tallys, but once I replaced them with ArrayLists that held the chains, it broke. Am I doing something wrong in the ArrayList code?
private void calculateIterations(){
for (long i = 1; i <= upperBound; i++){
calculateAnswer(i);
}
}
private void calculateAnswer(long i) {
long number = i;
ArrayList <Long> tempList = new ArrayList<>();
if (number == 1 ){
tempList.add(number);
if (tempList.size() > biggestList.size()){
biggestList.clear();
biggestList.addAll(tempList);
printLargest();
}
}
else if (number % 2 == 0){
tempList.add(number);
number /= 2;
calculateAnswer(number);
}
else {
tempList.add(number);
number = number * 3 + 1;
calculateAnswer(number);
}
}
private void printLargest(){
biggestList.stream().forEach((n) -> {
System.out.print(n + " → ");
});
System.out.println("");
System.out.println("That's " + biggestList.size() + " numbers long!");
}
List.