Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

BACKGROUND: Let\'s consider three rules: If a number is even , divide it by 2. I

ID: 3731141 • Letter: B

Question


BACKGROUND: Let's consider three rules:

If a number is even, divide it by 2.

If a number is odd, triple it and then add 1.

Stop if you ever get to 1.

For example, starting with 7:

or, starting with 12:

It has been conjectured (I suppose by someone named Ulam) that no matter what positive number we begin with, the pattern will always reach 1. This has been tested on trillions of numbers, and it has always worked, but no one has been able to prove that it works for all numbers.


TASK: Write a Python program that given some number determines how many numbers are in the sequence, or stated another way, determines how many "steps" (plus one) it took to get to one. For example, if given the number 12 (that is, reads a "12" from standard input), the program should determine that there are 10 numbers in the sequence (see second example above) and should print that result. Similarly, if given the number 7, the program should print that the sequence length is 17. The program should then read another number and repeat this process until a sentinel value of zero is detected.


IMPLEMENTATION NOTES: Define an integer value-returning function, named "ulam_sequence_length()", that when invoked with a single (positive) integer argument returns the Ulam sequence length of that integer. For example,

will assign a value of 17 to x.

Do not use any global variables. Read your data from standard input (stdin) using the input() function. Don't bother prompting the user for input as you will be using the UNIX input file redirection mechanism to obtain the input values from a file. Use as your source file name "ulam.py". Your output should consist of two columns of (right-justified) numbers: the first column is 12 characters wide and contains the input number; the second column is 6 characters wide and contains the number's corresponding Ulam sequence length.

Explanation / Answer

''' To read input until user enters 0 you can use while loop with always true condition and break statement inside it to break/stop loop when user enters 0 To check even / odd you can use % operator which leaves remainder as 0 if number is even when we divides by 2 ex- 15%2==1 => 15 is odd 14%2==0 => 14 is even Sample Input/Output: 7 7 17 12 12 10 ''' ###################################3 #Create a .py file and copy code from below line to the end of answer #Function to count length of sequence def ulam_sequence_length(number): count=1 while(number!=1): #incrementing count count +=1 #If number is even divide by 2 if(number%2==0): number=number//2 else: #number is odd multiply by 3 and add 1 number =number*3+1 #Return count return count #your main program start from here #Running loop until user enters 0 while(True): #Reading number from user as string and converting it to integer number=int(input()) #Checking if number is 0 , then stopping reading numbers if(number==0): break else: #Calling function to count lenght=ulam_sequence_length(number) #Displaying number and lenght print(number," ",lenght)