Ask your own question, for FREE!
Computer Science 14 Online
OpenStudy (curry):

hi guys, I don't know what I'm doing wrong, can you guys please help me? Given the rules for a sequence: If n is even: n = n / 2 If n is odd: n = 3n + 1 For example, if the starting number is 5 the sequence is: 5 -> 16 -> 8 -> 4 -> 2 -> 1 Find the largest starting number under ten million that will generate the longest run of this sequence and ends in 1.

OpenStudy (curry):

System.out.print("Till which number would you like to decode this sequence?"); Scanner x1 = new Scanner(System.in); int input = x1.nextInt(); System.out.print("Your number is"+input); for (int x = 1; x < input ; x += 2){ int y; y = (x * 3) +1; while (y<input){ int z ; z = y/2; if(z > 1) while (z>1){ z = y/2;} if (z < 1) break; if (z==1) break; System.out.print(+z); } }}}

OpenStudy (curry):

What am I doing wrong?

OpenStudy (espex):

To start with, your sequence will never evaluate an odd number. You start x at 1, then you add 2 for every iteration, so you are always stepping up odd numbers. Then you multiply by 3 and add 1, so y is always even. Since you run the evaluation on z, which is just y divided by 2, you are always working on an even increment. You could find your odd/even numbers more efficiently if you take your value and mod it by 2. For example: 5%2 = 1 6%2 = 0 25%2 = 1 100%2 = 0 As for your sequence, you are never generating one. Take an input of 10, your output for x,y,z would be: var 1st iteration 2nd iteration 3rd iteration 4th iteration 5th iteration x 1 3 5 7 9 y 4 10 16 22 28 z 2,1 5,2,1 8,4,2,1 11,5,2,1 14,7,3,1

OpenStudy (curry):

Well the 3rd iteration worked, correct? 5>16>8,4,2,1. So that's what I want, except the largest possible value.

OpenStudy (curry):

I set it so it will only print if it comes up. And how come when I enter this in the program, it doesn't display an answer for me?

OpenStudy (espex):

The iterations that I laid out were based on the mathematical logic you were using. I was attempting to avoid compiling your code as I was not clear on the end goal you were after. That said, I have compiled your code and you have a couple of infinite loops. 1. Your 'while' is true for as long as 'y<input' and yet the value of 'y' is never changed while (y<input){ int z ; z = y/2; if(z > 1) while (z>1){ z = y/2; } if (z < 1) break; if (z==1) break; System.out.print(+z); } 2. Your 'if' is true 'while' z>1, since 'y' is never changed, 'z' is never changed. This is where your program sticks. if(z > 1) while (z>1){ z = y/2; }

OpenStudy (espex):

If your desired output is something like this: Till which number would you like to decode this sequence?5 Your number is 5 16 8 4 2 1 I would suggest code similar to this: int x; x = input; while(x>1){ if (x%2 == 0){ x = (x/2); } else if (x%2 == 1){ x = (x * 3) + 1; } System.out.println(x); }

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!