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 3: Keep track of the maximum number of iterations Scanning this list of results causes us to ask the following question: What is the longest sequence? The easiest way to compute this is to keep track of the largest count seen so far. Each time we generate a new count, we check to see if it is larger than what we think is the largest. If it is greater, we update our largest so far and go on to the next count. At the end of the process, the largest seen so far is the largest of all Create a variable call maxSoFar and initialize it to zero. Place this initialization outside the for loop so that it only happens once. Now, inside the for loop, modify the code so that instead of printing the result of the seq3npl function, we save it in a variable, call it result. Then we can check result to see if it is greater than maxSoFar. If so, update maxSoFar Experiment with different ranges of starting values # 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
