Deseription: The Collatz sequence starts at a specified integer and ends at 1, w
ID: 3678943 • Letter: D
Question
Deseription: The Collatz sequence starts at a specified integer and ends at 1, with all numbers in-between being generated by the following rule applied to the previous number x in the sequence - /2if x is even 3a +1 if c is odd. 13x + 1 i ¢ is odd. Filename Specification: The solution for this problem must be coded in a file named one-py Input Specification: The first and only input must use the prompt "Enter the starting integer of the Collatz sequence: ". This input is supposed to take the starting integer from the user. Output Specification: The output must be the string "The Collatz sequence is " followed by comma & space separated sequence of the integers in the sequence. Sample Interaction (output from program is shown in red and user input shown in blue) Enter the starting integer of the Collatz sequence: 10 The Collatz sequence is 10, 5, 16, 8, 4, 2, 1Explanation / Answer
def collatz(nuii):
if nuii % 2 == 0:
print(nuii // 2)
return nuii // 2
elif nuii % 2 == 1:
resii = 3 * nuii + 1
print(resii)
return resii
n = input("Enter the starting integer of the Collatz sequence: ")
while n != 1:
n = collatz(int(n))