Question: def seq3np1(n): Print the 3n+1 sequence from n, terminating when it reaches 1. while n != 1: print(n) if n % 2 == 0:
def seq3np1(n):
""" Print the 3n+1 sequence from n, terminating when it reaches 1."""
while n != 1:
print(n)
if n % 2 == 0: # n is even
n = n // 2
else: # n is odd
n = n * 3 + 1
print(n) # the last print is 1
seq3np1(3)
--------------------------------------------------------------------------------------------------------
code it in python

Activity 1: Count the number of iterations it takes to stop Our program currently prints the values in the sequence until it stops at 1. Remember that one of the interesting questions is How many items are in the sequence before stopping at 1?. To determine this, we will need to count them. First, comment out (or delete) the print statements that currently exist. Now we will need a local variable to keep track of the count. It would make sense to call it count. It will need to be initialized to 0 since before we begin the loop. Once inside the loop, we will need to update count by 1 (increment), so that we can keep track of the number of iterations. It is very important that you put these statements in the right place. Notice that the previous location of the print statements can be very helpful in determining the location. When the loop terminates (we get to 1), return the value of count This demonstrates an important pattem of computation called a counter (note that it is a type of accumulator). The variable count is initialized to 0 and then incremented each time the loop body is executed. When the loop exits, count contains the result-the total number of times the loop body was executed. Since the function now returns a value, we will need to call the function inside of a print statement in order to see the result # copy and paste your code here
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
