Learning Scientific Programming with Python (2nd edition)
P2.5.7: 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).