1. Member
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.
This is the code I have so far:

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);
}
}
}```
There is a problem with the output and I'm not sure where I'm going wrong.

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
The bold numbers shouldn't be there, and I can't figure out why they are?

Thanks!

2. ## 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.

3. Member
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?

4. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
10

## Re: Loop troubleshooting

When you define an if-else 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.

5. ## Re: Loop troubleshooting

Originally Posted by Zigster
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?
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++;
}```

6. Member
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);
}
}```
Output sample:

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
It appears to work as I intended. d is storing the highest value of c, and e is storing the corresponding a value. But the final value of e is not the correct answer.

Where am I going wrong?

Thanks!

7. Member
Join Date
Apr 2012
Posts
74
Rep Power
0

## Re: Loop troubleshooting

I figured out my problem was that the numbers got too high for ints, so I changed it to longs and now it gives me the correct answer.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•