Files
CSC110/assignments/a4/a4.tex
T
2021-11-09 15:20:48 -05:00

281 lines
11 KiB
TeX

\documentclass[fontsize=11pt]{article}
\usepackage{amsfonts}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage[utf8]{inputenc}
\usepackage[margin=0.75in]{geometry}
\title{CSC110 Assignment 4: Number Theory, Cryptography, and Algorithm Running Time}
\author{Azalea Gui \& Peter Lin}
\date{\today}
% Some useful LaTeX commands. You are free to use these or not, and also add your own.
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\cO}{\mathcal{O}}
\newcommand{\floor}[1]{\left\lfloor #1 \right\rfloor}
\newcommand{\code}[1]{\texttt{#1}}
\begin{document}
\maketitle
\section*{Part 1: Practice with Proofs}
\begin{enumerate}
\item[1.] Statement to prove:
$$\forall a, k, n \in \Z,~ \gcd(a, n) = 1 \Rightarrow \gcd(a + kn, n) = 1$$
\begin{proof} : \\
$d = \gcd(a,n)$ is defined as $(a=0 \land n=0 \implies d=0) \land \\ (a \neq 0 \lor n \neq 0 \implies d | a \land d | n \land (\forall e \in \N, e | a \land e | n \implies e \le d))$ \\
\\
Let $a,k,n \in \Z$ \\
Assume that $\gcd(a,n) = 1$ \\
Since $d = 1 \neq 0$, this implies that $a \neq 0 \lor n \neq 0$ \\
Since $a \neq 0 \lor n \neq 0$, we know $1 | a \land 1 | n \land (\forall e_2 \in \N, e_2 | a \land e_2 | n \implies e_2 \le 1)$ is also true. \\
We need to prove: $(a + kn) \neq 0 \lor n \neq 0 \implies 1 | (a + kn) \land 1 | n \land (\forall e \in \N, e | (a + kn) \land e | n \implies e \le 1)$ \\
\\
Suppose $(a + kn) \neq 0 \lor n \neq 0$ \\
We need to prove: $1 | (a + kn) \land 1 | n \land (\forall e \in \N, e | (a + kn) \land e | n \implies e \le 1)$
\begin{enumerate}
\item[1.] Proving for: $1 | (a + kn)$ \\
That is: $\exists c \in \Z$ s.t. $(a + kn) = 1 \cdot c$ \\
Take $c = (a + kn)$ \\
$(a + kn) = 1 \cdot (a + kn)$ is true.
\item[2.] $1 | n$ is given to be true.
\item[3.] Proving for $\forall e \in \N, e | (a + kn) \land e | n \implies e \le 1$ \\
Let $e \in \N$ \\
Suppose $e | (a + kn) \land e | n$ \\
$a + kn = ex \land n = ey$ for some $x,y \in \Z$ \\
$a + key = ex$ for some $x,y \in \Z$ \\
$a = e(x - ky)$ for some $x,y \in \Z$ \\
Let $c = (x - ky)$ \\
By substitution, we now have: $a = ec$ \\
Therefore, $\exists c \in \Z$ s.t. $a = ec$ is true. \\
Which means $e | a$ is true. \\
Since we are given $\forall e_2 \in \N, e_2 | a \land e_2 | n \implies e_2 \le 1$ \\
Take $e_2 = e$, we now have $e | a \land e | n \implies e \le 1$ \\
And since we now know $e | a$ and $e | n$, we can conclude $e \le 1$. \\
Which is what we want to show.
\end{enumerate}
\end{proof}
\item[2.] Statement to prove (we've expanded the definition of Omega for you!):
$$\exists c, n_0 \in \R^+,~ \forall n \in \N,~ n \geq n_0 \Rightarrow \log_{3} n - \log_{11} n \geq c \cdot \log_{14} n$$
\begin{proof}
TODO: Your proof goes here.
\end{proof}
\item[3.] Statement to prove (we haven't expanded the definition of Big-O for you, but we encourage you to do so yourself):
$$\forall f, g: \N \to \R^{\geq 0},~ g \in \cO(f) \land \big(\forall m \in \N,~ f(m) \geq 1 \big) \Rightarrow g \in \cO(\floor{f})$$
\begin{proof}
TODO: Your proof goes here.
\end{proof}
\end{enumerate}
\newpage
\section*{Part 2: Generating Coprime Numbers}
\begin{enumerate}
\item[1.]
Not to be handed in.
\item[2.]
Complete this part in the provided \texttt{a4\_part2.py} starter file.
Do \textbf{not} include your solution in this file.
\item[3.]
Prove that each loop invariant holds.
\begin{enumerate}
\item[a.] Loop Invariant 1
\begin{proof} : \\
Variables: In this proof, $N$ is the abbreviation for the list \texttt{nums\_so\_far}. \\
\\
Assumption 1: The loop invariant 1 is true for the previous iteration. \\
That is $\forall k_2 \in N, gcd(k_2, 2) = 1 \land gcd(k_2, 3) = 1$ \\
\\
Assumption 2: The statement proven in Part 1.1: \\
$\forall a,k,n \in \Z, gcd(a,n) = 1 \implies gcd(a + kn, n) = 1$ \\
\\
We need to prove: $\forall k \in N \cup \{ N[-2] + 6 \}, gcd(k, 2) = 1 \land gcd(k, 3) = 1$ \\
Which is equivalent to: $\forall k \in N, gcd(k, 2) = 1 \land gcd(k, 3) = 1$ and \\
$gcd(N[-2] + 6, 2) = 1 \land gcd(N[-2] + 6, 3) = 1$ \\
\\
Since the first part is the same as the previous iteration, it is true. \\
What we need to prove becomes:
$gcd(N[-2] + 6, 2) = 1 \land gcd(N[-2] + 6, 3) = 1$ \\
\\
Pick $k_2 = N[-2] \in N$ \\
By assumption 1, we know that $gcd(N[-2], 2) = 1$ and $gcd(N[-2], 3) = 1$
\begin{enumerate}
\item[1.] Proving for $gcd(N[-2] + 6, 2) = 1$ \\
Pick $a = N[-2], k = 3, n = 2$ \\
Since we know $gcd(N[-2], 2) = 1$, $gcd(a, n) = 1$ is true. \\
Therefore, by assumption 2, we know that $gcd(a + kn, n) = 1$ is also true. \\
Substituting the varaibles back, we know $gcd(N[-2] + 6, 2) = 1$.
\item[2.] Proving for $gcd(N[-2] + 6, 3) = 1$ \\
Pick $a = N[-2], k = 2, n = 3$ \\
Since we know $gcd(N[-2], 3) = 1$, $gcd(a, n) = 1$ is true. \\
Therefore, by assumption 2, we know that $gcd(a + kn, n) = 1$ is also true. \\
Substituting the varaibles back, we know $gcd(N[-2] + 6, 3) = 1$.
\end{enumerate}
\end{proof}
\item[b.] Loop Invariant 2
\begin{proof} : \\
Variables: In this proof, $N$ is the abbreviation for the list \texttt{nums\_so\_far}, and $|N|$ represents the size of $N$. \\
\\
Assumption 1: The loop invariant 2 is true for the previous iteration. \\
That is $\forall i_2 \in \{ 0, \dots, |N| - 3\}, N[i_2] + 6 = N[i_2 + 2]$ \\
\\
Let $M = N \cup \{ N[-2] + 6 \}$ be the list of the current iteration. \\
We need to prove: $\forall i \in \{0, \dots, |M| - 3\}, M[i] + 6 = M[i + 2] $
\begin{enumerate}
\item[1.] Let $i < |M| - 3$ \\
Since the new entry added to $M$ is not included in $i$, this case is equivalent to the previous iteration, and we know that is true by assumption 1.
\item[2.] Let $i = |M| - 3$ \\
We need to prove: $M[|M| - 3] + 6 = M[|M| - 3 + 2]$ \\
That is $M[-3] + 6 = M[-1]$ \\
\\
Let's start with a true statement: \\
$N[-2] = N[-2]$ \\
Since $N[-2] = (N \cup \{ N[-2] + 6 \})[-3] = M[-3]$, \\
$M[-3] = N[-2]$ \\
Add 6 to both sides: \\
$M[-3] + 6 = N[-2] + 6$ \\
Since $M[-1] = (N \cup \{ N[-2] + 6 \})[-1] = N[-2] + 6$ is it's last entry, \\
$M[-3] + 6 = M[-1]$ \\
Which is what we want to show.
\end{enumerate}
\end{proof}
\item[c.] Loop Invariant 3
\begin{proof} : \\
Variables: In this proof, $N$ is the abbreviation for the list \texttt{nums\_so\_far}, and $|N|$ represents the size of $N$. \\
\\
Assumption 1: Loop invariant 3 is true for the previous iteration. \\
That is $\forall i_2 \in \{0, \dots, |N| - 2\}, N[i_2] < N[i_2 + 1]$ \\
\\
Assumption 2: Loop invariant 2 is true for the previous iteration. \\
That is $\forall i_3 \in \{0, \dots, |N| - 3\}, N[i_3] + 6 = N[i_3 + 2]$ \\
\\
Let $M = N \cup \{ N[-2] + 6 \}$ be the list of the current iteration. \\
We need to prove: $\forall i \in \{0, \dots, |M| - 2\}, M[i] < M[i + 1] $ \\
\\
Assumption 3: Loop invariant 2 is true for the current iteration. \\
That is $\forall i_4 \in \{0, \dots, |M| - 3\}, M[i_4] + 6 = M[i_4 + 2]$ \\
\\
Let's first prove an intermediate statment, statment 4: \\
$\forall i_5 \in \{0, \dots, |N| - 2\}, 0 < N[i_5 + 1] - N[i_5] < 6$ \\
Let $i_5 \in \{0, \dots, |N| - 2\}$, \\
We want to show $0 < N[i_5 + 1] - N[i_5] < 6$ \\
\\
Pick $i_2 = i_5$, \\
We know that $N[i_5] < N[i_5 + 1]$ by assumption 1. \\
Which means $0 < N[i_5 + 1] - N[i_5]$ \\
\\
Then, we need to prove $N[i_5 + 1] - N[i_5] < 6$ \\
Since $N = \text{list}[1, 5]$ before the first iteration, the base case $N[1] < N[0] + 6$ is true. \\
For the inductive step, let's look at the true statement again: \\
$N[i_5] < N[i_5 + 1]$ \\
Add 6 to both sides: \\
$N[i_5] + 6 < N[i_5 + 1] + 6$ \\
Pick $i_3 = i_5$ \\
We know that $N[i_5] + 6 = N[i_5 + 2]$ by asssumption 2. \\
By substitution, our true statement becomes: \\
$N[i_5 + 2] < N[i_5 + 1] + 6$ \\
$N[(i_5 + 1) + 1] < N[i_5 + 1] + 6$ \\
Which is the end of our induction. \\
\\
We have proven the intermediate statement 4. \\
We now need to prove: $\forall i \in \{0, \dots, |M| - 2\}, M[i] < M[i + 1] $
\begin{enumerate}
\item[1.] Let $i < |M| - 2$ \\
Since the new entry added to $M$ is not included in $i$, this case is equivalent to the previous iteration, and we know that is true by assumption 1.
\item[2.] Let $i = |M| - 2$ \\
We need to prove: $M[|M| - 2] < M[|M| - 2 + 1]$ \\
That is $M[-2] < M[-1]$ \\
\\
Pick $i_5 = |N| - 2$ \\
We know that $0 < N[-1] - N[-2] < 6$ by statement 4 \\
Since $N[-1] = (N \cup \{ N[-2] + 6 \})[-2] = M[-2]$, \\
Since $N[-2] = (N \cup \{ N[-2] + 6 \})[-3] = M[-3]$, \\
$0 < M[-2] - M[-3] < 6$ \\
$M[-2] < M[-3] + 6$ \\
Pick $i_4 = |M| - 3$ \\
We know that $M[-3] + 6 = M[-1]$ by assumption 3 \\
By substitution, we now have: \\
$M[-2] < M[-1]$
Which is what we want to show.
\end{enumerate}
\end{proof}
\item[d.] Loop Invariant 4
\begin{proof}
TODO: Your proof goes here.
\end{proof}
\end{enumerate}
\item[4.]
Complete this part in the provided \texttt{a4\_part2.py} starter file.
Do \textbf{not} include your solution in this file.
\item[5.]
Complete this part in the provided \texttt{a4\_part2.py} starter file.
Do \textbf{not} include your solution in this file.
\end{enumerate}
\newpage
\section*{Part 3: Running-Time Analysis}
\begin{enumerate}
\item[1.]
Running-time analysis of \texttt{coprime\_to\_2\_and\_3}.
Let $N$ be the list \code{nums\_so\_far}, and let $n$ be the input integer.
The loop in the function runs until $N[-2] + 6 < n$. Since $N = \text{list}[1, 5]$ before the first iteration, the loop will run $\text{(amount of coprimes below n)} - 2$ times. Since the loop appends $N[-2] + 6$ to $N$, $N[-2]$ will increase for the next iteration by either $5 - 1 = 4$ or $1 + 6 - 5 = 2$ in an alternating way, which is an increase of $3$ on average. This loop will run $(n - 6) / 3 = n / 3 - 2$ times on average. However, since $\cO(n / 3 - 2) = \cO(n)$, we can be conservative here and say that the loop will run at most $n$ times.
Since the loop contained only constant-time operations, the runtime of the loop is $\cO(n)$.
Since there are only constant-time operations outside the loop, the runtime of the entire function is $\cO(n)$.
\item[2.]
Running-time analysis of \texttt{starting\_coprime\_numbers}.
\item[3.]
TODO: Running-time analysis of \texttt{coprime\_to\_all}.
\end{enumerate}
\section*{Part 4: Two New Cryptosystems}
Complete this part in the provided \texttt{a4\_part4.py} starter file.
Do \textbf{not} include your solution in this file.
\end{document}