.py
files containing your work.This homework assignment must be submitted in Gradescope by Noon central time on Tuesday February 14, 2023.
Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.
This assignment is about recursion and Python notebooks. (Worksheet 5 also included material on context managers, but that isn't explicitly tested in this assignment.)
Most relevant:
Less likely to be relevant, but also allowed:
This homework assignment has 3 problems, numbered 2, 3, and 4. The grading breakdown is:
Points | Item |
---|---|
3 | Autograder |
4 | Problem 2 |
4 | Problem 3 |
4 | Problem 4 |
15 | Total |
The part marked "autograder" reflects points assigned to your submission based on some simple automated checks for Python syntax, etc. The result of these checks is shown immediately after you submit.
Ask your instructor or TA a question by email, in office hours, or on discord.
As with worksheet 5, for this homework assignment you'll be asked to complete all of your work in a Python notebook.
Submit everything as a single file called mcs275hw5.ipynb
.
If you work in Jupyter (as installed with python3 -m pip install notebook
), just name the notebook mcs275hw5
and it will be saved with the .ipynb
extension in the directory where you started Jupyter.
If you work in Colab, you can download the .ipynb
file to your computer using the menu bar that Colab displays at the top of the notebook. Select File, then Download, and in the submenu, Download .ipynb.
When your assignment is graded, this problem's score will reflect whether your notebook follows the notebook format guidelines below. The other problems contain coding exercises and the scores on those problems will look for correctness of your work.
#
as the formatting directive) and your name## Problem 3
in Markdown which renders as Problem 3 in large bold text)..py
file, and don't just rename a Python source file from .py
to .ipynb
. The way to make a .ipynb
file is to use the Python notebook interface.Finally, here's a rough picture of what the notebook you submit might look like (but obviously this one doesn't contain solutions:
Let $n$ be a positive integer. Let's define the tower factorial of $n$ to be
$$ n^{(n-1)^{(n-2)^{(n-3)^{\cdots^{2^1}}}}} $$That is, you raise $n$ to a certain power. The exponent is $(n-1)$ raised to a certain power, etc., until you end up at $1$.
By convention, we'll say the tower factorial of $1$ is $1$ and that the tower factorial of $0$ or of a negative integer doesn't exist.
Write two functions:
towerfact(n)
that computes the tower factorial using recursiontowerfact_iterative(n)
that computes the tower factorial using iteration (no recursion).Each function should raise a ValueError
if the given integer is zero or negative. Otherwise it should return the answer.
You can put these functions in a single notebook cell or in separate cells, as you prefer.
Also add cells to your notebook that show both functions being tested. At a minimum your tests should include
n
and towerfact(n)
for 1 <= n <= 5
n
and towerfact_iterative(n)
for 1 <= n <= 5
1 <= n <= 5
towerfact(4)
is correct, where the return value is compared to an expression that you enter manually to calculate the tower factorial.Note that the tower factorial of $4$ is the rather large number $$ 4^{3^{2^1}} = 4^{\left ( 3^{\left ( 2^1 \right)} \right )} = 4^{3^2} = 4^9$$
This is quite different from $$ \left ( \left(4^3 \right )^2 \right )^1$$ which is much smaller and which is more simply written as as $ 4^{3 \times 2 \times 1}$.
The number towerfact(6)
is so astronomically large that you should not expect it to be possible to compute on your computer. Python integers are limited in range only by available memory and time, so if you try to compute towerfact(6)
you'll probably just end up waiting forever or exhausting your computer's memory (which would make the Python interpreter stop).
Define an integer sequence $a_n$ as follows:
Thus it is similar in spirit to the Fibonacci sequence, but the rule is slightly different and depends on the parity (odd/even) of $n$.
To illustrate the definition, this sequence begins: $$ \begin{split} a_0 &= 1\\ a_1 &= 3\\ a_2 &= 5 = 2 a_1 - a_0\\ a_3 &= 10 = 1 + (a_1)^2\\ a_4 &= 35 = 4 a_3 - a_2\\ a_5 &= 101 = 1 + (a_3)^2 a_6 &= 571 = 6 a_5 - a_4 \end{split} $$
Write a recursive function aseq(n)
that computes and returns $a_n$. (If given a negative value of n
, the function should raise ValueError
.)
Include cells showing that you tested your function's return value for all $n$ from $0$ to $15$.