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

Pyhton programming... for intro to computers class Consider the following proced

ID: 3857745 • Letter: P

Question

Pyhton programming... for intro to computers class

Consider the following procedure:

Start with an integer x.

If x is even, then divide it by 2; if it's odd, instead multiply it by 3 and add 1.

Repeat this process until x = 1.

For example, starting with x=15 would give the sequence of numbers 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1. Write a program that has the user enter a value for x and then displays the resulting sequence.

To check whether a number is even you should use the % operator: if x is even, then x % 2 == 0 and if it's odd x % 2 == 1.

(Background: the Collatz conjecture is that regardless of what number you start with, you will eventually reach 1 through this procedure. Although it was formulated about 80 years ago, nobody has yet managed to prove or disprove it — it has been checked by computer up to x=6,000,000,000,000,000,000 and so far every one of them has had this behavior, but it is still not known whether it's true for allintegers. If you happen to find an x that doesn't go to 1, you could win some notoriety!)

Explanation / Answer

print "Enter number "
x=input()
print "Sequence is"
while x!=1:
   if x%2==0:
       x=x/2
   else:
       x=x*3+1
   print x

==============================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ python intnum.py
Enter number
15
Sequence is
46
23
70
35
106
53
160
80
40
20
10
5
16
8
4
2
1