Question: Write Python code which computes the Hailstone path length of a positive integer. The Hailstone function is defined as: h(n) : if n is even,

Write Python code which computes the Hailstone path length of a positive integer.

The Hailstone function is defined as:

h(n) : if n is even, n/2

if n is odd, 3*n+1

The Hailstone path for an integer is the sequence of numbers generated by the Hailstone function, beginning at and ending at 1. The Hailstone path length is the number of integers that appear on the Hailstone path, which is 1 plus the number of applications of the Hailstone function required to reach. For example, the Hailstone path for 12 is

12 6 3 10 5 16 8 4 2 1

Thus, the path length is 10. The function was applied 9 times.

It is conjectured that all positive integers eventually reach 1 after repeated application of the Hailstone function. Although the conjecture is believed to be true, it has not yet been proven.

Hint 1: use a while loop

Hint 2: Use integer division: // the double division slash

Hint 2: The input function returns a string you need to convert it to an integer using the int function like this:

n = input('Enter a value for n ') n = int(n)

Your output should look like this (for an input of 12):

Enter a value for n 12

12 6 3 10 5 16 8 4 2 1 The path length is 10 steps

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!