diff --git a/assignments/a4/a4.tex b/assignments/a4/a4.tex index 15fba23..c76743c 100644 --- a/assignments/a4/a4.tex +++ b/assignments/a4/a4.tex @@ -268,6 +268,20 @@ Since there are only constant-time operations outside the loop, the runtime of t \item[2.] Running-time analysis of \texttt{starting\_coprime\_numbers}. +Let $P$ be the size of the input set \code{primes}, and let $m$ be the product of the numbers in \code{primes}. + +Let $c_0, \dots, c_n$ be constants that doesn't depend on any variables. + +The inner loop \code{for p in primes} runs $P$ iterations, and since each iteration contains only a constant number of constant-time operations, taking $c_0P$ steps where $c_0$ is a constant. The outer loop iterates $m - 1$ times, taking $c_0P + c_1$ steps each time, with a total of $(m - 1)(c_0P + c_1)$ steps. + +Since there are only constant-time operations outisde the outer loop, the entire function will take $(m - 1)(c_0P + c_1) + c_2$ steps. + +Thus, the total number of basic operations is: +\begin{align} +RT_{\code{starting\_coprime\_numbers}}(P, m) &= (m - 1)(c_0P + c_1) + c_2 \\ +& = c_0mP + c_1m - c_0P + c_3 \\ +& \in \Theta(mP) +\end{align} \item[3.] TODO: Running-time analysis of \texttt{coprime\_to\_all}. \end{enumerate}