Question: Use Python Q2: Hailstone Recall the hailstone function from Lab 2. First, pick a positive integer n as the start. If n is even, divide

Use Python

Q2: Hailstone

Recall the hailstone function from Lab 2. First, pick a positive integer n as the start. If n is even, divide it by 2. If n is odd, multiply it by 3 and add 1. Repeat this process until n is 1. Write a recursive version of hailstone that prints out the values of the sequence and returns the number of steps.

Hint: When taking the recursive leap of faith, consider both the return value and side effect of this function.

# Question 2

def hailstone(n):

"""Print out the hailstone sequence starting at n, and return the

number of elements in the sequence.

>>> a = hailstone(10)

10

5

16

8

4

2

1

>>> a

7

"""

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!