Hey everyone, I've been working hard on this, and I don't know if I'm making progress. How do I get the number, (i) to go through the corresponding formula and THEN returning with the next number (i+1). If you know, Please help me out a bit. Also, if know how - how to determine the maximum number of interations, and the number that generated it. public class Collatz { public static void main(String [] args) { for (int count =1; count <= 65535; count++) { for (int i = 1; i <=65535; i++) { if( i% 2 == 0) { i = i / 2; } else { i = 3 * i + 1; } System.out.println(i); } } } } Thank you so much.
Yep, I noticed I was trying to answer your question in the other thread, and I gave this infinite loop, too (can't check the code atm, just going off the top of my head). I think this would stop the infinite loop, but I'm still unsure the question 100%. public class Collatz { public static void main(String [] args) { for (int count =1; count <= 65535; count++) { for (int i = 1; i <=65535; i++) { temp_i = i; if( temp_i% 2 == 0) { temp_i = temp_i / 2; } else { temp_i = 3 * temp_i + 1; } System.out.println(temp_i); } } } }
Now those two bits of code do very different things. The first is a nested loop of 64k iterations with each iteration going into a loop that runs about 10 times since I is never even during the if-else statement. so About 640K iterations total The second is a nested loop that does 64K iterations with each iteration doing 64K inner iterations. A total of about 4 billion iterations. Did you want to use that count variable somewhere? What is the codes intention?