Assignment 2

Goals

The goal of this assignment is to work on control structures and functions in Python.

Instructions

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.12 for your work. (Older versions may work, but your code will be checked with Python 3.12.) 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 or miniforge are probably the easiest ways to install and manage Python. If you work locally, you may launch Jupyter Lab either from the Navigator application (anaconda) or via the command-line as jupyter-lab or 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”. Because we are concentrating on control flow and functions, you may not use lists, other collections, or comprehensions for this assignment.

Due Date

The assignment is due at 11:59pm on Wednesday, September 18.

Submission

You should submit the completed notebook file required for this assignment on Blackboard. The filename of the notebook should be a2.ipynb.

Details

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.

0. Name & Z-ID (5 pts)

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.

1. The 3n + 1 Function (15 pts)

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. Use a loop to call your method on the inputs from 1 to 20 to produce 20 lines of results. 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
Hints
  • The print method has an optional keyword argument end that may be helpful in keeping the output on one line.
  • If the method gets stuck in an infinite loop, use the Interrupt Kernel menu item under the Kernel menu in JupyterLab.
  • The range method will be useful for the last part; check all of its parameters.

2. Up and Down Steps (10 pts)

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. Use a loop to call your method by on the inputs from 1 to 20, and then print those results in the format

<input>: <up steps> <down steps>

Sample output:

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
Hints:
  • Python allows multiple results to be returned from a function by putting commas between them

3. Longest Progression (15 pts)

Using the steps method, we can compute the stopping time of the orbit as the sum the number of up and down steps. 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 a meaningful way in this method. Calling this function on 100 returns 97, 118. Use a loop and this function to compute and print the results of the longest progression calculation for the inputs 15, 30, 45, 60, 75, 90, 105.

Hints
  • The range method will be useful here; check all of its parameters.
  • There can be multiple numbers that have the same stopping time.
  • Python allows multiple results to be returned from a function by putting commas between them
  • Choose a good method name
  • Remember that assignment expressions work well in control statements where the checked result is being used in the block