Results 1 to 7 of 7
Thread: Loop troubleshooting
 12052012, 04:05 PM #1Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
Loop troubleshooting
I'm trying to solve Problem 14 of the Euler Project website:
The following iterative sequence is defined for the set of positive integers:
n n/2 (n is even)
n 3n + 1 (n is odd)
Using the rule above and starting with 13, we generate the following sequence:
13 40 20 10 5 16 8 4 2 1
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
Which starting number, under one million, produces the longest chain?
NOTE: Once the chain starts the terms are allowed to go above one million.
Java Code:public class Problem14 { public static void main(String args[]){ for(int a=1; a<20; a++){ System.out.println("a: " + a); int b = a; int c = 1; while (b>1){ if (b % 2 == 0){ b = b/2; System.out.println("b1: " + b); c++; } else b = 3*b + 1; System.out.println("b2: " + b); c++; } System.out.println("c: " + c); } } }
For instance the output for a: 13 is:
a: 13
b2: 40
b1: 20
b2: 20
b1: 10
b2: 10
b1: 5
b2: 5
b2: 16
b1: 8
b2: 8
b1: 4
b2: 4
b1: 2
b2: 2
b1: 1
b2: 1
c: 17
Thanks!

Re: Loop troubleshooting
Always, always, ALWAYS enclose all blocks in curly braces. If you don't do this it will one day bite you on the bottom. For you, today is that day it seems.
 12052012, 04:40 PM #3Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
Re: Loop troubleshooting
Thanks Fubarable!
I was missing a curly bracket in my "else" statement, works now that I added it.
Can you explain to me what the code was doing without the curly bracket?
 12052012, 04:47 PM #4Senior Member
 Join Date
 Jun 2007
 Location
 Bali, Indonesia
 Posts
 758
 Rep Power
 8
Re: Loop troubleshooting
When you define an ifelse without curly braces only one line after the if or the else statement is considered as the body of the if or else statement. Whether you indent the expressions or not. So either you have one one statement of more place them inside curly braces.
Website: Learn Java by Examples

Re: Loop troubleshooting
Sure can. Without the curly brace, only one line is affected by the else, the line immediately below the else statement. What was happening was this:
Java Code:while (b>1){ if (b % 2 == 0){ b = b/2; System.out.println("b1: " + b); c++; } else { // this is called only if the if condition is false: b = 3*b + 1; } // this is *always* called System.out.println("b2: " + b); c++; }
 12052012, 05:43 PM #6Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
Re: Loop troubleshooting
Now I am having a new problem. I have tried to finish the code, but the website tells me I have the wrong answer. Again I cannot see what I have done wrong. In the example I have it just counting to 10 to test it.
Java Code:public class Problem14 { public static void main(String args[]){ int d = 1; int e = 0; for(int a=1; a<10; a++){ System.out.println("a: " + a); int b = a; int c = 1; while (b>1){ if (b % 2 == 0){ b = b/2; System.out.println("b1: " + b); c++; } else{ b = 3*b + 1; System.out.println("b2: " + b); c++; } } if (c>d){ d=c; e=a; } System.out.println("c: " + c); System.out.println("d: " + d + " e: " + e); } //System.out.println("d: " + d + " e: " + e); } }
a: 3
b2: 10
b1: 5
b2: 16
b1: 8
b1: 4
b1: 2
b1: 1
c: 8
d: 8 e: 3
a: 4
b1: 2
b1: 1
c: 3
d: 8 e: 3
a: 5
b2: 16
b1: 8
b1: 4
b1: 2
b1: 1
c: 6
d: 8 e: 3
a: 6
b1: 3
b2: 10
b1: 5
b2: 16
b1: 8
b1: 4
b1: 2
b1: 1
c: 9
d: 9 e: 6
Where am I going wrong?
Thanks!
 12072012, 02:28 PM #7Member
 Join Date
 Apr 2012
 Posts
 74
 Rep Power
 0
Similar Threads

Problem with while loop, assigning a variable with a different value every loop? Help
By JavaProg in forum New To JavaReplies: 2Last Post: 11072011, 02:25 AM 
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10012011, 02:12 AM 
troubleshooting
By ahhreeyell in forum AWT / SwingReplies: 2Last Post: 03182011, 02:54 AM 
Problems troubleshooting errors
By tnixon22 in forum New To JavaReplies: 29Last Post: 02212011, 01:42 AM 
JTextField loop 2x forloop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02162011, 05:46 PM
Bookmarks