Python Homework #4
Instructions:
Today's problems are related to the Collatz conjecture and hailstone sequences.
These are sequences of numbers that you get by using this rule:
For a number n:
- if n is odd, the next number is 3n+1
- if n is even, the next number is n/2
Applying this rule repeatedly starting from 5 would result in
5, 16, 8, 4, 2, 1.
-
Write a function hailstoneNext(n). This should take a
positive integer n and return the next number in the hailstone sequence after n.
-
Use your hailstoneNext to write a function printHailstone(n). This should take a
positive integer n and print out the hailstone sequence starting with n.
You should use a while loop, looping until n <= 1.
Make sure that your code prints out the final 1.
- Write one of the following two functions:
-
Write a function hailstoneMax(n). This should return the largest number
encountered on the way from n to 1.
For example, hailstoneLen(5) should return 16.
-
Or, write a function hailstoneLen(n). This should return the total number
of steps to get to 1. For example, hailstoneLen(5) should return 6.
- Play around with your print, len, or max functions. See if you can find some numbers that
give very long sequences or sequences that reach very high numbers. In the comments-to-teacher box, put an n you found, and how high/long the sequence got.
Bonus problem: write some functions to automatically search for the n that gives you the
highest/longest sequence, for n in the range 1 to 1000.