Results 1 to 7 of 7
  1. #1
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default Simple program, probably easy solution? [SOLVED!]

    My class' first assignment was to create a program that computes the hailstone sequence for an arbitrary number n. That was easy enough to do, heres my code:
    Java Code:
    package assignment01;
    
    import java.io.*;
    import java.util.Scanner;
    
    /**
     * This program computes hailstone sequence
     * 
     * @author Sean Repanshek
     * @version 1-10-2011
     */
    public class HailstoneSequence {
    	static Scanner scanner = new Scanner(System.in);
    	@SuppressWarnings("null")
    	public static void main(String[] args) throws IOException {
    		Scanner s = null;
    		int inputNum; // our variable to hold users number
    		int numIterations = 1; // number of iterations to get to zero
    		try {
    			System.out.println("Enter an integer: ");
    			inputNum = scanner.nextInt(); // get inputNum from user
    			System.out.println("You entered " + inputNum);
    			if (inputNum == 1) { // special case that inputNum is 1
    				numIterations = 0;
    				System.out.println("You entered 1");
    				System.out.println("Number of iterations: " + numIterations);
    				System.exit(inputNum);
    			} else if (inputNum % 2 == 1) { // if inputNum is odd to start, terminate (assignment criteria)
    				System.out.println("You entered an odd num. Terminated.");
    				System.exit(inputNum);
    				s.close(); // stop scanner
    			}
    			while (inputNum != 1) { // so long as inputNum is larger than 1, do hailstone algorithm
    				if (inputNum % 2 == 1) { // if inputNum is odd, make even according to algorithm
    					inputNum = inputNum * 3 + 1;
    					numIterations++; // increase iterations.
    					System.out.println(inputNum);
    				}
    				inputNum = inputNum / 2; // inputNum is even, divide by 2 for next part of algorithm
    				numIterations++; // increase iterations
    				System.out.println(inputNum);
    			}
    		} finally {
    			if (s != null) { // make sure everything terminates and
    				s.close(); // stop scanner
    			}
    		}
    		System.out.println("Number of repetitions to zero: " + numIterations); // print iterations in algor.
    	}
    
    }
    So that works fine and dandy for computing an even integers Hailstone sequence. I"m sure there are redundancies in it, but this is the first real java program I've written :P

    Now, my next part of the assignment is to figure out which integer is the largest that can be computed using this method before overflow happens with the int data type. using that as a rough template, I come out with:
    Java Code:
    package assignment01;
    
    /**
     * This program computes ...
     * 
     * @author Sean Repanshek
     * @version 1-10-2011
     */
    public class smallestHailstone {
    
    	public static void main(String[] args) {
    		int inputNum = 2; // our variable to hold users number
    		int nextNum = inputNum;
    		while (inputNum > 0) { // so long as inputNum is larger than 1, do hailstone algorithm
    			if (inputNum % 2 == 1) { // if inputNum is odd, make even
    										// according to algorithm
    				inputNum = inputNum * 3 + 1;
    
    			}
    			else {
    				inputNum = inputNum / 2; // inputNum is even, divide by 2 for next part of algorithm
    			}						
    			nextNum = inputNum;
    			inputNum++;
    			System.out.println(nextNum);
    			System.out.println(inputNum);
    			//System.out.println("Smallest number not computable: " + nextNum);
    		}
    	
    	}
    }
    and it definitely doesn't work. This is where I'm stumped... not quite sure what to do or how to go about increasing it until it hits overflow, and then prints the largest hailstone number that can be computed as an int.
    Hints?
    ****Solved. I'll post the solution on the last post. ****
    Last edited by falkon114; 01-18-2011 at 08:31 PM. Reason: figured out the solution

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I'm confused. In your post you mention finding the largest hailstone and yet your class is called smallestHailstone. Which is it?

    In either case if you want to find largest/smallest then you will need an if statement somewhere: if current number is larger/smaller than previous value, reset previous value to current number.

    Or maybe I still do not understand what you are trying to do. Then you need to explain yourself again.

  3. #3
    Prompt is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    figure out which integer is the largest that can be computed using this method before overflow happens with the int data type
    A easy brute force way is to do the following :

    Psuedo-Code:
    Java Code:
    do while not Done
      long currentHailStoneNumber = getNextHailStoneNumber();
      if(currentHailStoneNumber >= Integer.MAX_VALUE) output currentHailStoneNumber and quit

  4. #4
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Sorry about the confusion. The program is called "smallestHailstone" for this reason:
    From part #2 (make a program that x = 0 and y = -1, increment x++ until it's = to y) you should see that some integers cannot be computed in Java. This means that some Hailstone sequences cannot be properly computed using int variables in Java. You are to write an application that automatically determines the smallest integer that cannot have its Hailstone sequence computed using int variables.
    When your application is run, it should print a message like the following message:
    The integer (some integer here) cannot have its Hailstone sequence computed using int variables.
    So i guess i was thinking i needed to do something different =/ guess i didn't understand the program, but that there is my assignment, which, i now assume, is probably different than my initial question. Sorry!

  5. #5
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    So i'm still lost as to what I need to do next... The input has helped, but i still don't quite understand what I need to change, and where to put what... My most recent attempt has lead to...
    Java Code:
    package assignment01;
    
    public class largestHailstone {
    		public static void main(String[] args) {
    			int inputNum = 2; // our variable to hold users number
    			//int count = 1;
    			if (inputNum > 0) {
    				inputNum++;
    			
    			while (inputNum != 1) { // so long as inputNum is larger than 1, do hailstone algorithm
    				//inputNum++;
    				//count++;
    				if (inputNum < 0) {
    					break; 
    				}
    				if (inputNum % 2 == 1) { // if inputNum is odd, make even
    											// according to algorithm
    					inputNum = inputNum * 3 + 1;
    
    				}
    				else {
    					inputNum = inputNum / 2; // inputNum is even, divide by 2 for next part of algorithm
    				}
    				System.out.println (inputNum);
    			}
    			//System.out.println (inputNum);
    		
    		}
    	}
    }
    As you can see, it's quite a mess with all my attempts at controlling new variables, and placing things in different spots.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Suppose a current number n in the sequence still fits in an int. If it is even then n/2 also fits in an int but possibly 3*n+1 doesn't fit in an int. This is true if n > (Integer.MAX_VALUE-1)/3. Check for that value when n is odd and you're done.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    falkon114 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    Solved solved solved. Here's the code we were looking for...
    Java Code:
    public class largestHailstone {
    	public static void main(String[] args) {
    		int inputNum = 1;
    		int nextNum = inputNum;
    		while (true) {
    			inputNum = nextNum; //assign the variable being computed to the next integer
    			while (inputNum != 1) { // so long as inputNum is larger than 1, do hailstone algorithm
    				if (inputNum % 2 == 1) { // if inputNum is odd, make even according to algorithm
    					inputNum = inputNum * 3 + 1;
    				}
    				inputNum = inputNum / 2; // inputNum is even, divide by 2 for next part of algorithm
    				if (inputNum < 0) {
    					System.out.println("The first number that cannot have its Hailstone computed is: " + nextNum + ".");
    					System.exit(0);	
    				}
    			}
    			nextNum++; //increase next number for Hailstone computation.
    		}
    	}
    
    }

Similar Threads

  1. Replies: 4
    Last Post: 08-23-2010, 03:19 AM
  2. Probably a simple solution
    By Collinryans in forum New To Java
    Replies: 6
    Last Post: 06-27-2010, 03:36 AM
  3. Replies: 0
    Last Post: 06-14-2010, 05:21 AM
  4. Need a simple and efficient solution.
    By Samgetsmoney in forum New To Java
    Replies: 9
    Last Post: 02-20-2009, 01:18 AM
  5. Simple serverless chat solution
    By goodjonx in forum Networking
    Replies: 3
    Last Post: 01-07-2008, 03:25 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
  •