Experimenting With the 3n+1 Sequence¶
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.
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
countby 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
This demonstrates an important pattern of computation called a counter (note that it is a type of accumulator). The variable
countis 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.
Repeat the call to
seq3np1using 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.
Create a simple for loop using a loop variable called
startthat provides values from 1 up to 50. Call the
seq3np1function once for each value of
start. Modify the print statement to also print the value of
Use the turtle graphics to graph the number of iterations. This provides an interesting visual that allows you to see the relative number of iterations for each value. You will probably want to use setworldcoordinates to make your graph an appropriate scale. You should also use the turtle to write out the loop varable and the number of iterations if the number of iterations is more than 100.
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
maxSoFarand 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
seq3np1function, we save it in a variable, call it
result. Then we can check
resultto see if it is greater than
maxSoFar. If so, update
Experiment with different ranges of starting values.