The goal of this assignment is to work on control structures and functions in Python.
You will be doing your work in a Jupyter notebook for this
assignment. You may choose to work on this assignment on a hosted
environment (e.g. tiger)
or on your own local installation of Jupyter and Python. You should use
Python 3.9 or higher for your work. To use tiger, use the credentials
you received. If you work remotely, make sure to download the .ipynb
file to turn in. If you choose to work locally, Anaconda is the easiest way
to install and manage Python. If you work locally, you may launch
Jupyter Lab either from the Navigator application or via the
command-line as jupyter-lab
.
In this assignment, we will be working with sequences of numbers related to the Collatz Conjecture, sometimes called the \(3n+1\) problem. It states that for any positive integer, repeatedly applying the following piecewise function will eventually reach 1: \[\begin{equation} f(n) = \begin{cases} n/2 \text{ if $n$ is even} \\ 3n + 1 \text{ if $n$ is odd} \end{cases} \end{equation}\] Note that while this seems like a very simple problem, mathematicians have spent a lot of time working on it without a proof. Terry Tao, who proved one of the most recent results related to Collatz, has a nice presentation on the topic. Shizuo Kakutani has said that “A joke was made that this problem was part of a conspiracy to slow down mathematical research in the U.S.”
For all numbers checked so far, the conjecture holds. The orbit is the sequence of numbers generated from a starting number to 1, and the stopping time is the number of steps until reaching one. In this assignment, we will write some functions to compute orbits and their steps “up” and “down”.
The assignment is due at 11:59pm on Wednesday, February 9.
You should submit the completed notebook file required for this
assignment on Blackboard. The
filename of the notebook should be a2.ipynb
.
Please make sure to follow instructions to receive full credit. Use a markdown cell to Label each part of the assignment with the number of the section you are completing. You may put the code for each part into one or more cells.
The first cell of your notebook should be a markdown cell with a line for your name and a line for your Z-ID. If you wish to add other information (the assignment name, a description of the assignment), you may do so after these two lines. Because we are concentrating on control flow and functions, do not use lists, other collections, or comprehensions for this assignment.
Write a function orbit
that for any positive integer
n
, computes and prints the orbit of the above \(3n+1\) function, stopping at 1. Print the
numbers separated with a ^
character when the next number
is greater than the previous one (a step up), and a
v
character when the next is less (a step down).
For example, orbit(5)
would produce the result
5 ^ 16 v 8 v 4 v 2 v 1
. Test your method by calling it on
the inputs from 1 to 20. Sample output (scrolls horizontally):
1
2 v 1
3 ^ 10 v 5 ^ 16 v 8 v 4 v 2 v 1
4 v 2 v 1
5 ^ 16 v 8 v 4 v 2 v 1
6 v 3 ^ 10 v 5 ^ 16 v 8 v 4 v 2 v 1
7 ^ 22 v 11 ^ 34 v 17 ^ 52 v 26 v 13 ^ 40 v 20 v 10 v 5 ^ 16 v 8 v 4 v 2 v 1
8 v 4 v 2 v 1
9 ^ 28 v 14 v 7 ^ 22 v 11 ^ 34 v 17 ^ 52 v 26 v 13 ^ 40 v 20 v 10 v 5 ^ 16 v 8 v 4 v 2 v 1
10 v 5 ^ 16 v 8 v 4 v 2 v 1
11 ^ 34 v 17 ^ 52 v 26 v 13 ^ 40 v 20 v 10 v 5 ^ 16 v 8 v 4 v 2 v 1
12 v 6 v 3 ^ 10 v 5 ^ 16 v 8 v 4 v 2 v 1
13 ^ 40 v 20 v 10 v 5 ^ 16 v 8 v 4 v 2 v 1
14 v 7 ^ 22 v 11 ^ 34 v 17 ^ 52 v 26 v 13 ^ 40 v 20 v 10 v 5 ^ 16 v 8 v 4 v 2 v 1
15 ^ 46 v 23 ^ 70 v 35 ^ 106 v 53 ^ 160 v 80 v 40 v 20 v 10 v 5 ^ 16 v 8 v 4 v 2 v 1
16 v 8 v 4 v 2 v 1
17 ^ 52 v 26 v 13 ^ 40 v 20 v 10 v 5 ^ 16 v 8 v 4 v 2 v 1
18 v 9 ^ 28 v 14 v 7 ^ 22 v 11 ^ 34 v 17 ^ 52 v 26 v 13 ^ 40 v 20 v 10 v 5 ^ 16 v 8 v 4 v 2 v 1
19 ^ 58 v 29 ^ 88 v 44 v 22 v 11 ^ 34 v 17 ^ 52 v 26 v 13 ^ 40 v 20 v 10 v 5 ^ 16 v 8 v 4 v 2 v 1
20 v 10 v 5 ^ 16 v 8 v 4 v 2 v 1
print
method has an optional keyword argument end
that may be
helpful in keeping the output on one line.range
method will be useful for the last part; check all of its
parameters.Now, write a function steps
that for any positive
integer n
returns the number of up and
down steps in the orbit produced by the \(3n+1\) function. For example,
steps(5)
would return 1, 4
. Do not use the
print method in the steps
method. Test your method by
calling it on the inputs from 1 to 20. Write the results next to the
input (up steps first):
1: 0 0
2: 0 1
3: 2 5
4: 0 2
5: 1 4
6: 2 6
7: 5 11
8: 0 3
9: 6 13
10: 1 5
11: 4 10
12: 2 7
13: 2 7
14: 5 12
15: 5 12
16: 0 4
17: 3 9
18: 6 14
19: 6 14
20: 1 6
Using the steps
method, we can compute the stopping
time of the orbit as the sum the number of up and down steps. Now,
write a new method that, given an input number m
, returns
two results: the largest number which has the
maximum stopping time of all numbers between 1 and
m
and that stopping time. You must use the walrus
operator (:=
) in this method. For example, calling this
function on 100 returns 97, 118
. Use this function to
compute and print the results of this longest progression calculation
for the inputs 15, 30, 45, 60, 75, 90, 105.
range
method will be useful here; check all of its parameters.