Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Don't pick my questions if you try to copy from cheg or somewhere else. I need expert explain to me, Thanks. In this lab we

Don't pick my questions if you try to copy from cheg or somewhere else. I need expert explain to me, Thanks.

In this lab we will try to gain a bit more information about the 3n+1 sequence. We will start with the code from the chapter and make modifications. Here is the function so far.

defseq3np1(n):

""" Print the 3n+1 sequence from n, terminating when it reaches 1."""

whilen !=1:

print(n)

ifn %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)

Activity 1:Count the number of iterations it takes to stop.

Our program currentlyprintsthe values in the sequence until it stops at 1. Remember that one of the interesting questions isHow 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 itcount. It will need to be initialized to 0 since before we begin the loop.

Once inside the loop, we will need to updatecountby 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),returnthe value ofcount.

This demonstrates an important pattern of computation called acounter(note that it is a type of accumulator). The variablecountis initialized to 0 and then incremented each time the loop body is executed. When the loop exits,countcontains 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

Activity 2:Repeat the call toseq3np1using a range of values, up to and including an upper bound.

Now that we have a function that can return the number of iterations required to get to 1, we can use it to check a wide range of starting values. In fact, instead of just doing one value at a time, we can call the function iteratively, each time passing in a new value.

Creat a simple for loop using a loop variable calledstartthat provides values from 1 up to 50. Call theseq3np1function once for each value ofstart. Modify the print statement to also print the value ofstart.

# copy and paste your code here

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.

Creat a variable callmaxSoFarand initialize it to zero. Place this initialization outside thefor loopso that it only happens once. Now, inside the for loop, modify the code so that instead of printing the result of theseq3np1function, we save it in a variable, call itresult. Then we can checkresultto see if it is greater thanmaxSoFar. If so, updatemaxSoFar.

Experiment with different ranges of starting values.

# copy and paste your code here

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

(a) solve for P and (b) solve for t. A = Pert

Answered: 1 week ago

Question

Human brain is also known as the emotional brain?

Answered: 1 week ago

Question

This is the outer most carnival appendage?

Answered: 1 week ago

Question

Proximity of the brain tissue?

Answered: 1 week ago

Question

Human brain are.......?

Answered: 1 week ago

Question

How many lobes are in the cerebral cortex?

Answered: 1 week ago