Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By Fubarable
  • 1 Post By wsaryada
  • 1 Post By Fubarable

Thread: Loop troubleshooting

  1. #1
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.
    Zigster likes this.

  3. #3
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default 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.
    Zigster likes this.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Loop troubleshooting

    Quote Originally Posted by Zigster View Post
    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++;
                }
    Zigster likes this.

  6. #6
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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. #7
    Zigster is offline Member
    Join Date
    Apr 2012
    Posts
    74
    Rep Power
    0

    Default 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.

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  3. troubleshooting
    By ahhreeyell in forum AWT / Swing
    Replies: 2
    Last Post: 03-18-2011, 02:54 AM
  4. Problems troubleshooting errors
    By tnixon22 in forum New To Java
    Replies: 29
    Last Post: 02-21-2011, 01:42 AM
  5. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM

Posting Permissions

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