In: Computer Science
Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules: If the current number n is odd, the next number in the sequence is equal to 3 * n + 1 If the current number n is even instead, the next number in the sequence is equal to one hald of n (i.e., n divided by 2) We repeat this process until it produces 1 (which is where the "mathematical curiosity" comes into play; so far, mathematicians have been unable to prove that this process always (eventually) produces the value 1, but no one has been able to find a counterexample that doesn't eventually reach 1). A Hailstone sequence can begin with any positive integer. For example, the integer 6 produces the hailstone sequence 6, 3, 10, 5, 16, 8, 4, 2, 1 In the code cell below, use the input() command to read in an integer from the user (assume that this integer is always positive and greater than 1). Then use a loop (we recommend a while loop) and any extra variables that you may need to follow the rules above to generate a Hailstone sequence from your starting number. DO NOT print out the Hailstone numbers that you generate; instead, your code should print exactly one number when it finishes: the total number of values in the Hailstone sequence that you generated (including both the starting value and the final 1 that ends the sequence).
Examples
The starting value 2 produces the output 2 (internally, it generates the sequence 2, 1).
Please look at my code and in case of indentation issues check the screenshots.
------------main.py----------------
def main():
n = int(input("Enter the integer to begin(greater than
1): "))
count = 0
#to count the number of
values in the sequence
while n!= 1:
#if number is not equal to 1
count = count + 1
#increase the count by 1
if n%2 == 1:
#if the number is odd
n =
3*n+1 #next number is 3n+1
else:
#if number
is even
n =
n/2 #next number
is n/2
count = count + 1 #to
count the last number in sequence which is 1
print(count)
#print the count
main()
--------------Screenshots-------------------
----------------------Output------------------------------------
-----------------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.
Thankyou