The Collatz sequence is the sequence of numbers generated by repeatedly computin
ID: 3557725 • Letter: T
Question
The Collatz sequence is the sequence of numbers generated by repeatedly computing the value of the function
for n a positive integer, and stopping when n = 1.
So, for example, the Collatz sequence that begins with n = 6 is:
and the one that begins with n = 11 is:
Input. This is a function, so it must take a positive integer as its only parameter. You do not need to write a wrapping program to call it, nor do you need to check that the parameter is a positive integer; you may assume that.
Output. Print the Collatz sequence beginning with n. For example, the function call
Explanation / Answer
this may help you..
n= int(raw_input("Enter the beginning number: "))
i=0
print n
while n > 1:
if n%2 == 0: # if n is even
n = n/2
print n
else:
n = 3*n+1
print n
i += 1;
or)
while 1 == 1:
print("Pick an integer greater than one, any integer greater than 1")
x = int(input())
count = 0
while x != 1:
if (x%2) ==1 or x==0:
x = int(3*x+1)
else:
x = int(x/2)
print(x)
count = count + 1
print("The number of iterations was", count)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.