Project: Hailstone steps with recursion
In Section 4 you wrote Hailstone steps with a loop. Write the same function again, this time with recursion.
A hailstone sequence starts with a positive integer. If the number is even, divide it by two to get the next number. If it is odd, multiply it by three and add one. Repeat with each new number until you reach 1. Starting from 3, the sequence is 3, 10, 5, 16, 8, 4, 2, 1.
Write a recursive function named hailstone_step_recursive that takes the
starting number and returns how many steps it takes to reach 1. A starting value
of 1 takes 0 steps. A starting value of 3 takes 7 steps. The function does not
print anything. It returns a value:
answer = hailstone_step_recursive(1000)
print(answer)
Your function must call itself, and it cannot use a loop.
To get started, ask two questions. Which starting value needs no more work? For every other value, how does the number of steps from it relate to the number of steps from the next number in the sequence?
Save the file as hailstone_recursive.py.
When you finish, compare it with your loop version. Which is shorter? Which is easier to read? Python stops a function that calls itself more than about 1000 times in a row, so the recursive version cannot handle a starting value like 989345275647, whose sequence takes 1348 steps. The loop version has no such limit.
Show a solution
Try the project on your own first. This is one working solution, followed by its tests. Press Run to run the tests. Read them to see what thorough testing looks like, then change the solution and run again to watch a test fail.
Projects run on your own computer, so you tell us when you are done.