Hi, I am suppose to create a code that takes the values between 1-1000 and produces the hailstone sequence for them, then it prints out the initial value that produced the longest sequence along with the sequence's length. I have tried to use an array but it didn't seem to work, so I tried a simpler approach but it's not working either:

public class one {
	public static void main(String[]args){
		int first=1;
		int last=1000;
		int max=0;
		int length=0;
		
		while (first<=last){
			
			while (first!=1)
			{
			if (first%2== 0)
			{
			first= first / 2;
			length++;
			}
			if (first % 2 != 0)
			{
			first = (3 * first) + 1;
			length++;
			}
			}
		first++;
		break;
}	
			
			if (max < length)
			{
			max = length;
			}
		
			
		
		System.out.println(max+first);
			  

		}
			 
		
		
}

Thank you for any help!

Dani AI

Generated

Quick diagnosis and what was missing

The posted program mutates the outer loop counter and contains an early break, so some starting values are never fully tested. As noted, iterate the starting values with a stable loop variable and copy that into a separate variable used for the hailstone steps. Reset the length counter for every start, and when a new maximum is found save both the length and the starting value. Also pick a clear convention: count either the number of terms in the sequence (including the starting value and the final 1) or the number of steps to reach 1 — be consistent.

Correct, simple implementation (counts terms, includes start and final 1)

public class HailstoneMax {
  public static void main(String[] args) {
    int limit = 1000;
    int bestStart = 1;
    int maxLen = 0;

    for (int start = 1; start <= limit; start++) {
      long n = start;          // use long for intermediate values
      int len = 1;             // count the starting term
      while (n != 1) {
        n = (n % 2 == 0) ? (n / 2) : (3 * n + 1);
        len++;
      }
      if (len > maxLen) {
        maxLen = len;
        bestStart = start;
      }
    }

    System.out.println("Start: " + bestStart + "  Length: " + maxLen);
  }
}

Extra notes and further improvements

For much larger ranges, memoize lengths for numbers below a chosen threshold (an int[] cache) so a previously computed tail can be reused and the loop terminates early when a cached value is reached. Stay mindful of overflow: long is safer than int for intermediate values, and BigInteger is needed only for extremely large experiments. To print the actual sequence for the best start, rerun the hailstone loop from that start and output n at each step.

Recommended Answers

All 4 Replies

Are you sure that you need to find for each number from 1 to 1000??? I know that any starting number will eventually reach "1" at the end of computation.

Anyway, line 17 should be "else" instead of "if" alone.

Are you sure that you need to find for each number from 1 to 1000??? I know that any starting number will eventually reach "1" at the end of computation.

Anyway, line 17 should be "else" instead of "if" alone.

Yes, I am sure. It says a program that finds the starting value in the range 1 to 1000 to produce the longest sequence.
I already changed the 'if' to 'if else'. I am trying to play around with it but I can't seem to get it to work.

OK, I'm done with the code. The answer for the number is 871 with 178 length.

Your "first" variable is tampered and cause the loop to go infinitely. To be sure that your loop counter wouldn't be tampered, you should use a "for" loop, so it is clearer.

for (int i=2; i<last; i++) {  // the "i" is the starting number of each Hailstone
    first = i;
    length = 0;
    while (first>1) {
      ...
    }
    // after the loop, check whether or not the new length is greater than the
    // current max you have. If so, replace it, and also keep the number "i"
    // as the biggest number as well
  }

OK, I'm done with the code. The answer for the number is 871 with 178 length.

Your "first" variable is tampered and cause the loop to go infinitely. To be sure that your loop counter wouldn't be tampered, you should use a "for" loop, so it is clearer.

for (int i=2; i<last; i++) {  // the "i" is the starting number of each Hailstone
    first = i;
    length = 0;
    while (first>1) {
      ...
    }
    // after the loop, check whether or not the new length is greater than the
    // current max you have. If so, replace it, and also keep the number "i"
    // as the biggest number as well
  }

Fantastic! Thanks a lot, you've made me understand loops much better than the teacher. It's so simple though, I feel like an idiot, but thanks a bunch. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.