Results 1 to 7 of 7
 01132011, 08:56 PM #1Member
 Join Date
 Dec 2010
 Posts
 18
 Rep Power
 0
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 1102011 */ 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. } }
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 1102011 */ 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); } } }
Hints?
****Solved. I'll post the solution on the last post. ****Last edited by falkon114; 01182011 at 09:31 PM. Reason: figured out the solution
 01142011, 12:37 AM #2
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.
 01142011, 03:47 AM #3Member
 Join Date
 Jan 2011
 Posts
 11
 Rep Power
 0
figure out which integer is the largest that can be computed using this method before overflow happens with the int data type
PsuedoCode:
Java Code:do while not Done long currentHailStoneNumber = getNextHailStoneNumber(); if(currentHailStoneNumber >= Integer.MAX_VALUE) output currentHailStoneNumber and quit
 01142011, 06:31 AM #4Member
 Join Date
 Dec 2010
 Posts
 18
 Rep Power
 0
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.
 01162011, 01:14 AM #5Member
 Join Date
 Dec 2010
 Posts
 18
 Rep Power
 0
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); } } }
 01162011, 08:57 AM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
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_VALUE1)/3. Check for that value when n is odd and you're done.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
 01182011, 09:31 PM #7Member
 Join Date
 Dec 2010
 Posts
 18
 Rep Power
 0
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

Simple SQL update doesn't work :( (Probebly easy error)
By Addez in forum New To JavaReplies: 4Last Post: 08232010, 03:19 AM 
Probably a simple solution
By Collinryans in forum New To JavaReplies: 6Last Post: 06272010, 03:36 AM 
Solution to Create Multiple Form and Switch between them Easy
By vdparaliya in forum Java AppletsReplies: 0Last Post: 06142010, 05:21 AM 
Need a simple and efficient solution.
By Samgetsmoney in forum New To JavaReplies: 9Last Post: 02202009, 02:18 AM 
Simple serverless chat solution
By goodjonx in forum NetworkingReplies: 3Last Post: 01072008, 04:25 PM
Bookmarks