1. Home
  2. Computing & Technology
  3. Java

The Collatz Theory

By , About.com Guide

Collatz Theory Solution

As the theorem has been proven for a ridiculously high number already, the question was to find out which number in the range 1 to 1000 generates the largest sequence of numbers and display it to the screen. The answer is the number 871:

871 2614 1307 3922 1961 5884 2942 1471 4414 2207
6622 3311 9934 4967 14902 7451 22354 11177 33532 16766
8383 25150 12575 37726 18863 56590 28295 84886 42443 127330
63665 190996 95498 47749 143248 71624 35812 17906 8953 26860
13430 6715 20146 10073 30220 15110 7555 22666 11333 34000
17000 8500 4250 2125 6376 3188 1594 797 2392 1196
598 299 898 449 1348 674 337 1012 506 253
760 380 190 95 286 143 430 215 646 323
970 485 1456 728 364 182 91 274 137 412
206 103 310 155 466 233 700 350 175 526
263 790 395 1186 593 1780 890 445 1336 668
334 167 502 251 754 377 1132 566 283 850
425 1276 638 319 958 479 1438 719 2158 1079
3238 1619 4858 2429 7288 3644 1822 911 2734 1367
4102 2051 6154 3077 9232 4616 2308 1154 577 1732
866 433 1300 650 325 976 488 244 122 61
184 92 46 23 70 35 106 53 160 80
40 20 10 5 16 8 4 2 1
There were 179 numbers in the sequence for the number 871.

Here's my version of the program:

public class CollatzTest {

  public static void main(String[] args) {

    int biggestNumber = 1;
    int biggestSequence = 1;
    int sequenceCount = 0;

    //Call the sequence generator 1000 times.
    for(int i=1;i<1001;i++)
    {
      sequenceCount = calculateSequence(i,false);

      if (sequenceCount ] biggestSequence)
      {
        biggestSequence = sequenceCount;
        biggestNumber = i;
      }
    }

    sequenceCount = calculateSequence(biggestNumber,true);
    System.out.println("\nThere were " + sequenceCount + " numbers in the sequence for the number " + biggestNumber + ".");
  }

  //The hailstone sequence generator is now in a method
  private static int calculateSequence(int number, boolean shouldDisplay)
  {
    int calculation = 1;
    if (shouldDisplay == true)
    {
      System.out.print(number + " ");
    }
    while (number > 1)
    {

      if (number % 2 == 0)
      {
        //Even number
        number /= 2;
      }
      else
      {
        //Odd number
        number = (number*3) + 1;
      }
      calculation++;
      if (shouldDisplay == true)
      {
        //Display nicely by wrapping around every 10th calculation
        if (calculation % 10 == 0)
        {
          System.out.println(number);
        }
        else
        {
          System.out.print(number + " ");
        }
      }
    }
    return calculation;
  }
}
Explore Java
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Java
  4. Programming Exercises
  5. Beginner Level
  6. The Collatz Theory>

©2010 About.com, a part of The New York Times Company.

All rights reserved.