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 Google Colab) or on your own local installation of Jupyter and Python. You should use Python 3.8 or higher for your work. To use tiger, use the credentials you received. For Colab, you will need to use a Google account. 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.”
In 2018, David Barina noted the \(7x\pm1\) problem seems to be a close relative of the Collatz Conjecture. It is also conjectured 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} \\ 7n + 1 \text{ if } n \equiv 1 \mod 4 \\ 7n - 1 \text{ if } n \equiv 3 \mod 4 \end{cases} \end{equation}\] The orbit is the sequence of numbers generated from a starting number to 1, and the stopping time, the number of steps until reaching one, is also interesting to consider. In this assignment, we will write some functions to compute orbits and their stopping times.
The assignment is due at 11:59pm on Monday, February 1.
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 \(7x\pm1\) function documented by Barina, stopping at 1. For example, orbit(5)
would produce the result 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
. Print the numbers with commas in between them, but no comma at the end. Test your method by calling it on the inputs from 1 to 20. Sample output (scrolls horizontally):
1
2, 1
3, 20, 10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
4, 2, 1
5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
6, 3, 20, 10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
7, 48, 24, 12, 6, 3, 20, 10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
8, 4, 2, 1
9, 64, 32, 16, 8, 4, 2, 1
10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
11, 76, 38, 19, 132, 66, 33, 232, 116, 58, 29, 204, 102, 51, 356, 178, 89, 624, 312, 156, 78, 39, 272, 136, 68, 34, 17, 120, 60, 30, 15, 104, 52, 26, 13, 92, 46, 23, 160, 80, 40, 20, 10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
12, 6, 3, 20, 10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
13, 92, 46, 23, 160, 80, 40, 20, 10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
14, 7, 48, 24, 12, 6, 3, 20, 10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
15, 104, 52, 26, 13, 92, 46, 23, 160, 80, 40, 20, 10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
16, 8, 4, 2, 1
17, 120, 60, 30, 15, 104, 52, 26, 13, 92, 46, 23, 160, 80, 40, 20, 10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
18, 9, 64, 32, 16, 8, 4, 2, 1
19, 132, 66, 33, 232, 116, 58, 29, 204, 102, 51, 356, 178, 89, 624, 312, 156, 78, 39, 272, 136, 68, 34, 17, 120, 60, 30, 15, 104, 52, 26, 13, 92, 46, 23, 160, 80, 40, 20, 10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
20, 10, 5, 36, 18, 9, 64, 32, 16, 8, 4, 2, 1
Now, write a function stopping_time
that for any positive integer n
returns the stopping time of the orbit produced by the \(7x\pm1\) function. Recall that this is the number of steps required to get to 1. For example, stopping_time(5)
would return 10. Do not use the print method in the stopping_time
method. Test your method by calling it on the inputs from 1 to 20. Write the results next to the input:
1: 0
2: 1
3: 13
4: 2
5: 10
6: 14
7: 18
8: 3
9: 7
10: 11
11: 53
12: 15
13: 19
14: 19
15: 23
16: 4
17: 27
18: 8
19: 50
20: 12
Using the stopping_time
method, 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 its stopping time. You must use the walrus operator (:=
) in this method. For example, calling this function on 100 returns 70, 326
. Use this function to compute and print the results of this longest progression calculation for the inputs 10, 20, 30, 40, 50, 60, 70, 80, 90, 100.
range
method will be useful here; check all of its parameters.