Java

  1. Home
  2. Computing & Technology
  3. Java
photo of Paul Leahy

Paul's Java Blog

By Paul Leahy, About.com Guide to Java

Monday's Programming Question

Monday October 20, 2008

Last week's programming question introduced Lothar Collatz's theory: for any number it's possible to make a sequence of numbers that will eventually end in one by following a simple rule; if the number is even halve it by two, if it's odd times it by three and add one (e.g., starting with the number 5 the sequence would be 5 16 8 4 2 1).

This theory is still unproven and I thought it might be interesting to expand the hailstone sequence generating program into one that could prove it for a certain set of numbers. Unfortunately we could never write a program that could prove the theorem completely because we would have to test all the numbers to infinity. However in terms of Java, it's a nice way to take a piece of code you've already written, move it into a method, and write some new code to call that method a number of times.

The question is can your program find the number between 1 and 1000 that has the longest sequence of numbers (e.g., the number 125 has 109 numbers in its sequence) and display it to the screen?

Hint: Take your code from last week or my version and move it into a method. Then use the new method to calculate the hailstone sequence for the numbers 1 to 1000 and keep track of which one is the longest. Finally, you'll need to modify the code so that it only displays the sequence to the screen for the right number.

Comments

No comments yet. Leave a Comment

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss

Community Forum

Explore Java

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Java

  1. Home
  2. Computing & Technology
  3. Java

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

All rights reserved.