Advertisement
Mazenkay

Untitled

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