Advertisement
Guest User

Untitled

a guest
May 1st, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.32 KB | None | 0 0
  1. \documentclass{article}
  2.  
  3. \title{COMP2111 Assignment 2 \\ Solutions}
  4. \date{1-5-2016}
  5. \author{Mazen Kourouche (z5059155), Anna Azzam (z3470058)}
  6. \usepackage{amsmath}
  7. \usepackage{units}
  8. \usepackage{amssymb}
  9. \usepackage{mathtools}
  10. \usepackage{enumerate}
  11. \usepackage{xcolor}
  12.  
  13. \begin{document}
  14. \pagenumbering{gobble}
  15. \maketitle
  16. \newpage
  17. \pagenumbering{arabic}
  18.  
  19.  
  20.  
  21. \section{Specification Statement}
  22. The function is specified by:
  23. \begin{equation*}
  24. proc \enspace Em \enspace (value \enspace n, result \enspace E)
  25. \end{equation*}
  26. Specification statement is:
  27. \begin{equation*}
  28. i : [n \in \mathbb{N} \enspace , \enspace E = Emirp(n)]
  29. \end{equation*}
  30. Where:
  31. \begin{equation*}
  32. Emirp(N) = \forall i \in [2, N] \enspace \cdot \enspace (isPrime(i) \enspace \land \enspace isPrime(reverse(i)) \enspace \land \enspace (i \neq reverse(i))
  33. \end{equation*}
  34. \begin{equation*}
  35. isPrime(X) = \forall i \in [2, X) \cdot ((x \enspace mod \enspace i) \neq 0)
  36. \end{equation*}
  37. And assuming reverse(I) is a method provided which reverses a number I
  38.  
  39.  
  40.  
  41.  
  42.  
  43. \section{Refinement}
  44. We started off with our specification statement and used refinement laws to derive our C function emirp. Any implications required during the refinement are proved below. \newline \newline
  45.  
  46. \subsection{Proof for main refinement}
  47.  
  48. Let \textbf{P} be equivalent to $ (nEmirps \leq n) \land (i \geq 2) $ \textbf{(Loop invariant)}. \newline
  49.  
  50. $i : [n \in \mathbb{N} , \enspace Emirp(n)]$ \newline\\
  51. $\sqsubseteq \enspace var\enspace nEmirps : \mathbb{N} \enspace \cdot \enspace \textcolor{red}{\llcorner} i, nEmirps : [n \in \mathbb{N}, \enspace Emirp(n)] \textcolor{red}{\lrcorner} $ \textbf{(i-loc rule)} \newline
  52. $\textcolor{red}{\sqsubseteq} \enspace \textcolor{blue}{\llcorner} i, nEmirps : [n \in \mathbb{N}, \enspace P] \textcolor{blue}{\lrcorner}
  53. \enspace \textbf{;} \enspace \textcolor{green}{\llcorner} i, nEmirps :[P, \enspace Emirp(n)] \textcolor{green}{\lrcorner}$ \textbf{(seq)} \newline
  54. $ \textcolor{blue}{\sqsubseteq} \enspace i, nEmirps : [n \in \mathbb{N}, \enspace P[\nicefrac{0}{nEmirps}]] \enspace \textbf{;} \enspace
  55. i, nEmirps [P[\nicefrac{0}{nEmirps}], \enspace P] $ \textbf{(seq)} \newline
  56. $\sqsubseteq \enspace nEmirps := 0\enspace ; \enspace i, nEmirps : [P[\nicefrac{0}{nEmirps}], \enspace P] $ \textbf{(ass, see impl. 1)} \newline
  57. $\sqsubseteq \enspace nEmirps := 0 \enspace \textbf{;} \enspace i := 2 $ \textbf{(ass, see impl. 2)} \newline\\
  58. $\textcolor{green}{\sqsubseteq} \enspace while \enspace (nEmirps < n) \enspace do \newline
  59. \indent \textcolor{green}{\llcorner} i, nEmirps : [(nEmirps < n ) \land P, \enspace P] \textcolor{green}{\lrcorner} \newline
  60. done $ \textbf{(while rule, see impl. 3)} \newline\\
  61. $\textcolor{green}{\sqsubseteq} \enspace var \enspace j : \mathbb{N} \enspace \cdot \enspace \textcolor{green}{\llcorner} i, j, nEmirps:[(nEmirps < n) \land P, \enspace P] \textcolor{green}{\lrcorner} $ \textbf{(i-loc)} \newline
  62. $\textcolor{green}{\sqsubseteq} \enspace i, j, nEmirps : [(nEmirps < n) \land P, \enspace P \land ((i != j) \vee (i = j))] \enspace \textbf{;} \newline
  63. \indent \indent \indent i, j, nEmirps : [P \land (i = j \vee i != j), \enspace P] $ \textbf{(seq)} \newline
  64. $\sqsubseteq \enspace j := reverse(i) \enspace \textbf{;} \newline
  65. \indent \indent \indent \textcolor{green}{\llcorner} i, j, nEmirps : [P \land (i = j \vee i != j), \enspace P] \textcolor{green}{\lrcorner} $ \textbf{(ass, see impl. 4)} \newline
  66. $\textcolor{green}{\sqsubseteq} \enspace i, j, nEmirps : [P \land (i = j \vee i != j), \enspace P[\nicefrac{i+1}{i}]] \enspace \textbf{;} \newline
  67. \indent \indent \indent i, j, nEmirps : [P[\nicefrac{i+1}{i}], \enspace P] $ \textbf{(seq)} \newline
  68. $\sqsubseteq \textcolor{green}{\llcorner} i, j, nEmirps : [P \land (i = j \vee i != j), \enspace P[\nicefrac{i+1}{i}]] \textcolor{green}{\lrcorner} \enspace \textbf{;} \newline
  69. \indent \indent \indent i := i + 1 $ \textbf{(ass, see impl. 5)} \newline\\
  70. $\textcolor{green}{\sqsubseteq} \enspace if \enspace (i != j) \enspace then \newline
  71. \indent \textcolor{green}{\llcorner} i, j, nEmirps :[P \land (i != j), \enspace P[\nicefrac{i+1}{i}]] \textcolor{green}{\lrcorner} \newline
  72. fi $ \textbf{(if1, see impl. 6)} \newline\\
  73. $\textcolor{green}{\sqsubseteq} \enspace i, j, nEmirps : [P \land (i != j) \enspace \land \enspace ((isPrime(i) \land isPrime(j)) \newline \indent \indent \indent \vee (\neg isPrime(i) \vee \neg isPrime(j))), \enspace P[\nicefrac{i+1}{i}]] $ \textbf{(w-pre, see impl. 7)} \newline\\
  74. $\sqsubseteq \enspace if \enspace (isPrime(i) \land isPrime(j)) \enspace then \newline
  75. \indent \textcolor{green}{\llcorner} i, j, nEmirps : [P \land (i!= j) \land (isPrime(i) \land isPrime(j)), \enspace P[\nicefrac{i+1}{i}]] \textcolor{green}{\lrcorner} \newline
  76. fi $ \textbf{(if 1, see impl. 8)} \newline\\
  77. $\textcolor{green}{\sqsubseteq} \enspace nEmirps := nEmirps + 1 $ \textbf{(ass, see impl. 9)} \newline\\
  78.  
  79. \noindent \textbf{Therefore the final code is:} \newline
  80. $var \enspace nEmirps \newline
  81. var\enspace i \newline
  82. nEmirps := 0 \newline
  83. i := 2 \newline
  84. while \enspace (nEmirps < n) \enspace do \newline
  85. \indent var j \newline
  86. \indent j := reverse (i) \newline
  87. \indent if \enspace (i != j) \enspace then \newline
  88. \indent \indent if \enspace (isPrime(i) \land isPrime(j)) \enspace then \newline
  89. \indent \indent \indent nEmirps := nEmirps + 1 \newline
  90. \indent \indent fi \newline
  91. \indent fi \newline
  92. \indent i := i + 1 \newline
  93. od
  94. $
  95.  
  96.  
  97. \subsection{Proof for isPrime}
  98.  
  99. $\gamma:[n \in \mathbb{N}, \enspace \gamma = isPrime(n)]$\\\\
  100. $\sqsubseteq \enspace var \enspace i: \mathbb{N} \enspace \cdot \enspace \textcolor{green}{\llcorner}\gamma, i:[n \in \mathbb{N}, \gamma = isPrime(n)] \textcolor{green}{\lrcorner}$ \textbf{(i-loc rule)} \\
  101. $\textcolor{green}{\sqsubseteq} \enspace \gamma, i:[n \in \mathbb{N}, \gamma = isPrime(i) \wedge i < n];$\\
  102. $ \indent\gamma, i:[\gamma = isPrime(i) \wedge i<n, \gamma = isPrime(n)]$\\
  103. $\indent \gamma:[n \in \mathbb{N}, \enspace \gamma = isPrime(n)]$\\\\
  104. $\enspace\sqsubseteq \enspace var \enspace i, iP: \mathbb{N} \cdot \textcolor{blue}{\llcorner}\gamma, \, iP: [n \in \mathbb{N}, \gamma = isPrime(n, i, iP)]\textcolor{green}{\lrcorner}$ \textbf{(i-loc rule)} \\
  105. $\textcolor{blue}{\sqsubseteq} \enspace \gamma, i, iP: \textcolor{red}{\llcorner}[n \in \mathbb{N}, \gamma = isPrime(n, i, iP) \enspace \wedge \enspace iP \in [0,1] \wedge i \leqslant n]\textcolor{red}{\lrcorner}$ \textbf{(s-post)}\\
  106. $\textcolor{red}{\sqsubseteq} \enspace \gamma, i, iP: [n \in \mathbb{N}, iP \in [0,1] \wedge i \leqslant n];$\\
  107. $\indent \enspace \textcolor{purple}{\llcorner}\gamma, i, iP: [\enspace iP \in [0,1] \wedge i \leqslant n \enspace \wedge \enspace \gamma = isPrime(n, i, iP)]\textcolor{purple}{\lrcorner}$ \textbf{(seq)}\\
  108. $\textcolor{purple}{\sqsubseteq} \enspace i:=0$ \textbf{(ass)} \\
  109. $\indent iP:=1$ \textbf{(ass)} \\
  110. $\indent \gamma, i, iP:[n \in \mathbb{N}, iP \in [0,1] \wedge i \leqslant n]$\\\\
  111. $\sqsubseteq \enspace while \enspace(i < n) \enspace do \enspace P \enspace od$
  112. $P:[i<n \wedge i \in \mathbb{N}, iP \in [0,1]]$ \textbf{(while rule)}\\\\
  113. $\noindent \sqsubseteq \enspace if ((Mod(n, i) = 0) \enspace then \enspace \textcolor{green}{\llcorner}w:[(Mod(n,i) = 0 \enspace \wedge \enspace (i<n \enspace \wedge \enspace i \in \mathbb{N}), \enspace iP = 0]\textcolor{green}{\lrcorner} \enspace else \enspace \textcolor{blue}{\llcorner}w:[(Mod(n,i)!=0) \enspace \wedge \enspace i < N \enspace \wedge i \in \mathbb{N} \enspace \wedge \enspace iP \in [0,1] \enspace \wedge \enspace i \leqslant n]\textcolor{blue}{\lrcorner} \enspace fi$ \textbf{(if)}\\\\
  114. $\sqsubseteq \enspace iP:=1$ \textbf{(ass)} \\
  115. $w:[(Mod(n,i) = 0 \enspace \wedge (i < n \wedge i \in \mathbb{N}), iP = 0]$\\\\
  116. $\sqsubseteq \enspace i:=i+1$ \textbf{(ass)} \\
  117. $w:[(Mod(n,i) != 0 \enspace \wedge i < n \wedge i \in \mathbb{N} \enspace \wedge iP \in [0,1], i \leqslant n]$\\\\
  118. \textbf{Therefore the final code is:}\\
  119. $var \enspace i$\\
  120. $ var \enspace iP$\\
  121. $i:=0$\\
  122. $iP:=1$\\
  123. $while (i<n) do$\\
  124. \indent $if (Mod(n,i) == 0) \enspace then$\\
  125. \indent\indent$iP:=0$ \enspace $else $\\
  126. \indent \indent $ i:=i+1$\\
  127. \indent$fi$\\
  128. $od$
  129.  
  130.  
  131.  
  132.  
  133. \subsection{Implications}
  134. \subsubsection{Implication 1}
  135. To use the \textbf{assignment rule}, we need to prove that $w = w_0$ which is true since E does not change, and \newline
  136. $pre \Rightarrow post[\nicefrac{e}{w}]$, \newline
  137. i.e. $n \in \mathbb{N} \enspace \Rightarrow \enspace P[\nicefrac{0}{nEmirps}][\nicefrac{0}{nEmirps}]$ \newline\\
  138. Assuming LHS true, RHS $\Leftrightarrow (0 \leq n \land i \geq 2)$ \newline
  139. $\Leftrightarrow True \land True$ since
  140. \begin{itemize}
  141. \item Pre-condition states $n \in \mathbb{N} $ therefore $n \geq 0$
  142. \item i is initialised to be 2 and is never decremented, therefore $i \geq 2$
  143. \end{itemize}
  144. $\therefore pre \Rightarrow post[\nicefrac{e}{w}]$ is true, justifying the use of the assignment rule.
  145.  
  146.  
  147. \subsubsection{Implication 2}
  148. To use the \textbf{assignment rule}, we need to prove that $w = w_0$ which is true, and
  149. $pre \Rightarrow post[\nicefrac{e}{w}]$, \newline
  150. i.e. $P[\nicefrac{0}{nEmirps}] \Rightarrow P[\nicefrac{2}{i}]$ \newline\\
  151. LHS $\Rightarrow n \geq 0 \land i \geq 2$ \newline
  152. $\therefore$ Assuming LHS true, RHS $\Leftrightarrow$ \newline
  153. $ (nEmirps \leq n) \land (2 \geq 2)$ \newline
  154. $\Leftrightarrow True \land True$ since
  155. \begin{itemize}
  156. \item Loop guard ensures nEmirps $<$ n \newline
  157. $\therefore$ nEmirps $\leq$ n is always true
  158. \item $2 \geq 2$ is obviously true
  159. \end{itemize}
  160. $\therefore pre \Rightarrow post[\nicefrac{e}{w}]$ is true, justifying the use of the assignment rule.
  161.  
  162.  
  163. \subsubsection{Implication 3}
  164. To use the \textbf{while rule}, we need to prove that $[P, Emirp(n)]$ is equivalent to $[inv, inv \land \neg g]$ where \newline
  165. \begin{itemize}
  166. \item $ g = (nEmirps < n)$, and
  167. \item inv = P
  168. \end{itemize}
  169. PRE-CONDITIONS: $P \Leftrightarrow inv$ is obviously true. \newline\\
  170. POST-CONDITIONS: Show that \newline
  171. $Emirp(n) \Rightarrow P \land \neg(nEmpirps < n)$ \newline
  172. $RHS \Leftrightarrow (nEmirps \leq n) \land (i \geq 2) \land (nEmirps \geq n)$ \newline
  173. $\Leftrightarrow (nEmirps = n) \land (i \geq 2)$ \newline
  174. $\Leftrightarrow True \land True \Leftrightarrow True$, since
  175. \begin{itemize}
  176. \item Loop only exits once $nEmirps = n$, \newline
  177. $\therefore nEmirps = n$ is true.
  178. \item i is initialised to be 2 and is never decremented, therefore $i \geq 2$
  179. \end{itemize}
  180. $\therefore LHS \Rightarrow RHS$, justifying the use of the while rule.
  181.  
  182.  
  183. \subsubsection{Implication 4}
  184. To use the \textbf{assignment rule}, we need to prove that $w = w_0$ which is true, and
  185. $pre \Rightarrow post[\nicefrac{e}{w}]$, \newline
  186. i.e. $P \land (i = j \vee i != j) \Rightarrow P[\nicefrac{reverse(i)}{j}]$ \newline\\
  187. $LHS \Rightarrow (P \land True)$, since $(i = j \vee i != j)$ is always true by \textbf{and with complement}. \newline
  188. $\Rightarrow True$ \newline\\
  189. $RHS \Rightarrow (nEmirps \leq n) \land (i \geq 2)$ \newline
  190. $\Leftrightarrow P$ \newline
  191. $\therefore LHS \Rightarrow RHS$ is true.
  192.  
  193.  
  194. \subsubsection{Implication 5}
  195. We need to show that $pre \Rightarrow post[\nicefrac{i+1}{i}]$ \newline
  196. i.e.$ P[\nicefrac{i+1}{i}] /Rightarrow P[\nicefrac{i+1}{i}]$ \newline
  197. which is obviously true.
  198.  
  199.  
  200. \subsubsection{Implication 6}
  201. To use the \textbf{if1 rule}, we need to prove that \newline
  202. $[P \land (i = j \vee i != j), \enspace P[\nicefrac{i+1}{i}]]$ implies \newline
  203. $[(g \land \phi) \vee (\neg g \land \psi[\nicefrac{w}{w_0}], \enspace \psi]$, where
  204. \begin{itemize}
  205. \item $\psi \enspace is \enspace P[\nicefrac{i+1}{i}]$, and
  206. \item $g \enspace is \enspace (i != j)$
  207. \end{itemize}
  208. POST-CONDITIONS: $P[\nicefrac{i+1}{i}] \Rightarrow \psi$ is obviously true. \newline\\
  209. PRE-CONDITIONS: Let $\psi \Leftrightarrow True$. \newline
  210. $RHS \Rightarrow (i != j \land True) \vee (i = j \land P[\nicefrac{i+1}{i}]$ \newline
  211. $\Rightarrow (i != j) \vee ((i = j) \land P[\nicefrac{i+1}{i}])$ \newline
  212. $\therefore LHS \Rightarrow RHS$ since \newline
  213. $i \geq 2 \Rightarrow i \geq 1 \newline \therefore P \Rightarrow P[\nicefrac{i+1}{i}]$ is True.
  214. Thus justifying the use of the if1 rule.
  215.  
  216.  
  217. \subsubsection{Implication 7}
  218. To prove the use of the \textbf{w-pre rule} we need to show that \newline
  219. $P \land (i != j) \Rightarrow \newline
  220. \indent P \land (i != j) \land ((isPrime(i) \land isPrime(j)) \vee (\neg isPrime(i) \vee \neg isPrime(j)))$ \newline\\
  221. Assuming LHS is true, \newline
  222. $RHS \Leftrightarrow True \land ((isPrime(i) \land isPrime(j)) \vee (\neg isPrime(i) \vee \neg isPrime(j)))$ \textbf{(by subbing in LHS)} \newline
  223. $\Leftrightarrow True \land True$ \textbf{(and with complement)} \newline
  224. $\Leftrightarrow True, \enspace \therefore LHS \Rightarrow RHS$ is True, justifying the use of w-pre.
  225.  
  226.  
  227. \subsubsection{Implication 8}
  228. To prove the use of the \textbf{if1 rule} we need to prove that \newline
  229. $[P \land (i != j) \land ((isPrime(i) \land isPrime(j)) \vee (\neg isPrime(i) \vee \neg isPrime(j))), \enspace P[\nicefrac{i+1}{i}]]$ \newline
  230. implies \newline
  231. $[(g \land \phi) \vee (\neg g \land \psi[\nicefrac{w}{w_0}], \enspace \psi]$, where
  232. \begin{itemize}
  233. \item $\psi \enspace is \enspace P[\nicefrac{i+1}{i}]$, and
  234. \item $g \enspace is \enspace (isPrime(i) \land isPrime(j))$
  235. \end{itemize}
  236. POST-CONDITIONS: $P[\nicefrac{i+1}{i}] \Rightarrow \psi$ is obviously true. \newline\\
  237. PRE-CONDITIONS: Let $\psi \Leftrightarrow True$. \newline
  238. $LHS \Rightarrow P \land (i != j) \land ((isPrime(i) \land isPrime(j) \vee \neg(isPrime(i) \land isPrime(j))$ \textbf{(De Morgan's law)} \newline
  239. $\Rightarrow P \land (i != j) \land True$ \textbf{(And with complement)} \newline
  240. $\Rightarrow P \land (i != j)$ \newline
  241. $RHS \Rightarrow ((isPrime(i) \land isPrime(j)) \vee (\neg(isPrime(i) \land isPrime(j)) \land P[\nicefrac{i+1}{i}])$ \newline
  242. $ \Rightarrow True \land P[\nicefrac{i+1}{i}]$ \newline
  243. $\Rightarrow True$ since $P \Rightarrow P[\nicefrac{i+1}{i}]$ is true.
  244.  
  245.  
  246.  
  247. \subsubsection{Implication 9}
  248. To use the \textbf{assignment rule}, we need to prove that $w = w_0$ which is true, and
  249. $pre \Rightarrow post[\nicefrac{nEmirps + 1}{nEmpirps}]$, \newline\\
  250. $RHS \Rightarrow (nEmirps + 1 \leq n) \land (i + 1 \geq 0)$ \newline
  251. $\Rightarrow (nEmirps < n) \land (i > 0)$ \newline
  252. Which is true when LHS is true since
  253. \begin{itemize}
  254. \item $nEmirps \leq n \Rightarrow nEmirps < n$ is true, and
  255. \item $i \geq 0 \Rightarrow i > 0$ is true, by logic.
  256. \end{itemize}
  257. Hence justifying the use of the assignment rule.
  258.  
  259.  
  260.  
  261. \section{Solution Questions}
  262. \subsection{Requirements}
  263. \subsubsection{Preconditions}
  264. \begin{itemize}
  265. \item The given value $n$ is a natural number
  266. \end{itemize}
  267. \subsubsection{Postconditions}
  268. \begin{itemize}
  269. \item $i$ is the $n^{th}$ Emirp
  270. \item $Emirp(n)$ is defined as $n^{th}$ Emirp in the domain of $\mathbb{N}$ (natural numbers)
  271. \item $n$ is greater than or equal to 2
  272. \item The $n^{th}$ Emirp, $Emirp(n)$ is the $n^{th}$ prime number $i$, such that the reverse of $i$ is also a prime number
  273. \item $i$ is not a palindrome
  274. \item $Emirp(n)$ is between 2 and $N$
  275. \end{itemize}
  276. \subsubsection{Invariant}
  277. \begin{itemize}
  278. \item The variable $nEmirps$ is less than or equal to $n$
  279. \item The variable $i$ is greater than or equal to 2
  280. \end{itemize}
  281.  
  282.  
  283. \subsection{Formal Specification and Requirements}
  284. \subsubsection{Overall Specification}
  285.  
  286. \begin{itemize}
  287. \item \(i:[ precondition , postcondition ]\)
  288. \begin{itemize}
  289. \item Here our specification is in the form of a program which stores the result in \(i\)
  290. \item The program \(E\) is such that once run with the preconditions, it should terminate successfully with a given postcondition.
  291. \end{itemize}
  292.  
  293. \end{itemize}
  294.  
  295.  
  296.  
  297.  
  298. \subsubsection{Preconditions}
  299.  
  300. \begin{itemize}
  301. \item \(n\in \mathbb{N}\)
  302. \begin{itemize}
  303. \item \(n\) is an element of the natural numbers.
  304. \item This therefore accounts for the fact that \(n\) must be greater than or equal to 0.
  305. \end{itemize}
  306.  
  307. \end{itemize}
  308.  
  309.  
  310.  
  311. \subsubsection{Postconditions}
  312.  
  313.  
  314. \begin{itemize}
  315. \item $i = Emirp(n)$
  316. \begin{itemize}
  317. \item This indicates that i is equal to the Emirp of $n$ which is also the $n^{th}$ Emirp
  318. \end{itemize}
  319. \item $Emirp(n) \in [2,n]$
  320. \begin{itemize}
  321. \item This indicates that the $n^{th}$ Emirp is between 2 and $N$
  322. \item This also indicates that $n$ is an element of $\mathbb{N}$, the natural numbers
  323. \end{itemize}
  324. \item $n \geqslant 2$
  325. \begin{itemize}
  326. \item This explains that $n$ must be greater than or equal to 2
  327. \item This therefore also indicates that 2 can not be considered an Emirp
  328. \end{itemize}
  329. \item $\forall i \in [2, N] \enspace \cdot \enspace (isPrime(i) \enspace \land \enspace isPrime(reverse(i)) \enspace \land \enspace (i \neq reverse(i))$
  330. \begin{itemize}
  331. \item This notes that the $i$ must be a prime number thna that its reverse, $reverse(i)$ must also be prime
  332. \item This part of the specification also mentions that $i$ can not be a palindrom since it is not equal to its own reverse
  333. \end{itemize}
  334.  
  335. \end{itemize}
  336.  
  337. \subsection{Changes in Translation to C Code}
  338. The changes we made in the translation from the derived code to the C program were minor:
  339.  
  340. \begin{itemize}
  341. \item We added a main function to read in input from STDIN and print output to STDOUT.
  342. \item We declared the variables as unsigned long - this adds a limitation, restricting the size of the input number to the size of an unsigned long.
  343. \item Our of a reverse function changed - the one from our derived program was a function $reverse(n)$ which returns the reversed number, while the function $reversen(n, *r)$ takes in a pointer to the result.
  344. \item When proving $isPrime(n)$, we noticed a great inefficiency. When proving the its correctness, we found our specification continued to iterate through numbers from $i$ through $n$, even when the number was already classed as 'prime'.
  345.  
  346. \end{itemize}
  347.  
  348.  
  349. \subsection{Tests - C Code}
  350. We tested our code on CSE servers and made runtime comparisons. This demonstrated the level of efficiency of our code:\\
  351.  
  352. \noindent\textbf{Results}\\
  353. Input: 1 Time: 0.000s\\
  354. Input: 10 Time: 0.000s\\
  355. Input: 50 Time: 0.008s\\
  356. Input: 100 Time: 0.048s\\
  357. Input: 200 Time: 0.332s\\
  358. Input: 300 Time: 0.576s\\
  359. Input: 400 Time: 0.808s\\
  360. Input: 1000 Time: 11.769s\\
  361. Input: 2000 Time: 29.822s\\\\
  362. By running the program against a variety of inputs, the average runtimes can be seen. The program calculates the emirp by starting at 1 and incrementing until n emirps have been found, which demonstrates why the time increases quite rapidly as input size increases - the program has O(n) complexity.
  363.  
  364.  
  365. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement