Does anybody know how I can write a code to do the following. 1.) From the numbers: 1 - 65536 , put them into a loop so that if number (n) is even they get divided by 2 ( n /2) or if odd, 3 * (n) +1. I want the result.. to be the put into the same equation and to continue until the pattern f(1) = 4, f(4)=2, f(2) = 1. Ex: f(10) = 5, then f(5) = 16, then f(16) = 8, etc. 2) Only output which number (#) created the most instances (chains) like shown in the example. 3) Output which number (#) it actually was. I don't need the exact code, but maybe an explanation as to how I would perform this loop, and loop the answer back into the equation. As well as how do I figure out the maximum, and which number did it? Thank you so much.
for (int i = 1; i <= 65536; i++){ if(i%2 == 0){ i = i / 2; } else { i = 3 * i + 1; } } I think that's what you want, unless you want i to start with some input value besides 1. Because this is always going to go 1, 4, 2, 1, I think. If you want i to have another value, then just set i = otherinputvalue instead of 1 at the loop declaration. I don't have time for the other 2 questions at the moment, sorry...maybe later.
Yeah you understood what I meant perfectly. I'm now stuck on how to make it end at 1, as opposed to continuing with the 1,4,2 cycle. And also making it continue on with i = 2, i =3, all the way to i = 65536 Please reply back when you're able. Thanks a lot by the way.
Ok, reading these questions more I would change my program int [] chainlength; //length of chain chainlength = new int [65536]; for (int i = 1; i <= 65536; i++){ initialvalue = i; chainlength = 1; do{ if(i%2 == 0){ i = i / 2; } else { i = 3 * i + 1; } chainlength++; }while(i != 1); System.out.println("Starting value: " + initialvalue + ", chain length: " + chainlength); } So for i = 1, it prints Starting value: 1, chain length: 4 Since it would be 1, 4, 2, 1. i = 2... Starting value: 2, chain length: 2 Since its just 2, 1 That's assuming the chain stops once it hits 1, since I guess they will all go to 1. If you want the chain to stop once it hits the same number it started with just change the do while to i != initialvalue If you want to find the highest chain length, just search through chainlength for the highest number. I hope that helps.
Actually have to change the code, there is an infinite loop int [] chainlength; //length of chain chainlength = new int [65536]; for (int i = 1; i <= 65536; i++){ chainlength = 1; initialvalue = i; //don't want to change i's value or we get an infinite loop //so set it to a temp value temp_i = i; do{ if(temp_i%2 == 0){ temp_i = temp_i / 2; } else { temp_i = 3 * temp_i + 1; } chainlength++; }while(temp_i != 1); System.out.println("Starting value: " + initialvalue + ", chain length: " + chainlength); }
So you're using a temporary variable so that the i value doesn't get changed and therefore corrupt the loop? The only problem is, I'm not allowed to use arrays, only the basics.. For loops, While loops, etc.
Then what you will have to do, instead of the array, set a "greatest chain" int to 0, and a "current chain" which is incremented each pass of the loop. If current chain > greatest chain, greatest chain = current chain. Also, if you need to know which number did the greatest chain... if (current chain > greatest chain){ greatest chain = current chain; greatest chain created number = i; } ... ... ... System.out.println("Greatest chain: " + greatest chain + " by: " + greatest chain created number); Are we just doing your homework or something? Why can't you use arrays?
I'm taking a Programming class, just for my own personal enjoyment at my local community college, and the students have a little assignment. Mine won't be graded since I'm not officially part of the College (just coming back free of charge ) but I still like to follow the requirements and not use methods that haven't been taught yet. Maybe if you could debug this code it would be easier.. since I'm pretty far. Thank you very much, I appreciate you taking your time, since you aren't obligated. public class Collatz { public static void main(String [] args) { int count = 1; int maximum=0; int i; int a; int n = 1; a = n; for (n = 1; n < 65536; n++) { while (a != 1) { if( n % 2 == 0) { a = n / 2; } else if (n % 2 != 0) { a = 3 * n + 1; } count++; } if (count > maximum) { maximum = count; } } System.out.println("The maximum number of iterations is: " +maximum); System.out.println("The number that generated this is: " +count); } }
I can't run this right now, I might try to do it later, but I need some more specifications... 1) Does the chain stop when it reaches 1? i.e. f(10) = 5, 16, 8, 4, 2, 1 If not, it will continue forever. Finally, will the output be something like f(1) = 4, 2, 1 Or 1, 4, 2, 1 Or what format? Does it even have to display each iteration of the loop? Or just the final value, something like: The number 120 had the highest chain size of 18 Something like that? Describe the output exactly, just write out what you want displayed. I think the highest chain will be the largest number that, divided by 2 = odd number. 65534. If not, 65535.
Basically it just needs to internally compute how many iterations each number (from 1 -65536) generates. As well as internally computing which number generates the most iterations/ consecutive cycles. Also, the number that did this. Ex: The number 18 f (18) = 9 , f (9) = 28, f(28) = 14 , f(14) = 7, f(7) = 22, etc.... Each one of those is an iteration so if this one takes say.. 15 iterations to reach "1" then the number 18 generated 15 iterations. So once it stops at one, it then continues onto the next number. The only thing that needs to be outputted (printed) is the maximum number of iterations and the number that generated it. Thank you.
Sorry, I forgot about this. I got this solution /* * LongestChain.java * * Created on October 9, 2007, 8:31 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /** * * @author Mike */ public class LongestChain { /** * @param args the command line arguments */ public static void main(String[] args) { //the current chain count in the loop //not the maximum. Set to 1 to start, //unsure if the number 2, for example, //is only a 1 chain or 2 chain (if we count the original //number or not. If we do not, set to 0 here //and in the bottom of the loop int current_chain_count = 1; //the maximum chain count, set //when the current chain > maximum chain. //Does not indicate a tie, if there is a tie, //the first number to get the maximum chain //is the only number reported int maximum_chain_count = 0; //the current number, but this is the number being //applied to the formula (div 2 if mod 2 = 0, * 3 + 1 if not, //i.e. if the current number is 1, this is set to 1, then becomes //4, 2, 1. int current_chain_number; //the number that caused the greatest chain, //only set when current chain > maximum chain //Does not account for ties. int number_creating_max_chain = 0; //Loops through all the numbers for(int current_number = 1; current_number <= 65536; current_number++){ //sets a temp current number, so we don't get into an infinite //loop current_chain_number = current_number; //do while current chain num != 1, since 1 //would not enter the loop do{ //applies transformation based on if the number is //even or odd if(current_chain_number%2 == 0){ current_chain_number = current_chain_number / 2; } else { current_chain_number = (current_chain_number * 3) + 1; } //chain increases, this should work as //2, 1 chain 2. current_chain_count++; } while(current_chain_number != 1); //if this chain is greater then max chain if(current_chain_count > maximum_chain_count){ //set max number and max chain maximum_chain_count = current_chain_count; number_creating_max_chain = current_number; } //reset chain count current_chain_count = 1; } System.out.println("The maximum number of iterations is: " + maximum_chain_count); System.out.println("The number that generated this is: " + number_creating_max_chain); } } It gave me The maximum number of iterations is: 340 The number that generated this is: 52527