# The Hailstone Sequence

#### Question P2.5.7

The hailstone sequence starting at an integer $n > 0$ is generated by the repeated application of the three rules:

• if $n=1$, the sequence ends;
• if $n$ is even, the next number in the sequence is $n/2$;
• if $n$ is odd, the next number in the sequence is $3n+1$.

(a) Write a program to calculate the hailstone sequence starting at 27.

(b) Let the stopping time be the number of numbers in a given hailstone sequence. Modify your hailstone program to return the stopping time instead of the numbers themselves. Adapt your program to demonstrate that the hailstone sequences started with $1 \le n \le 100$ agree with the Collatz conjecture (that all hailstone sequences stop eventually).

#### Solution

To access solutions, please obtain an access code from Cambridge University Press at the Lecturer Resources page for my book (registration required) and then sign up to scipython.com providing this code.