Kotlinlearncs.online LogoJava
Return to List

Solve: Hailstone Sequence

Created By: Justin Maier
/ Version: 2024.8.0

A "hailstone sequence" is defined as follows: Given any starting positive integer, the next term in the sequence is obtained from the previous by the following two rules:

  • If the previous term is even, the next term is equal to the previous term divided by two.
  • If the previous term is odd, the next term is equal to three times the previous term, plus one (3*previous + 1).

These sequences concern one of the most famous unsolved problems in mathematics: the Collatz conjecture, named after German mathematician Lothar Collatz who posed the question in 1937. The Collatz conjecture hypothesizes that, no matter the starting number, all such sequences eventually reach the number 1.

Given a positive int variable n, write a program that prints out each term in the hailstone sequence starting from n until 1 is reached (i.e. 1 should always be the last term printed).

Related Lessons

Stuck? You may find these lessons helpful: