From d89b4e54d7b4ee8aee05051ccf6e3b96019ac3ea Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 24 Jul 2016 18:04:15 +0200 Subject: Add exercise: [▶02] Saturated subtraction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- doc/exercises.tex | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'doc') diff --git a/doc/exercises.tex b/doc/exercises.tex index 85e7439..cf222db 100644 --- a/doc/exercises.tex +++ b/doc/exercises.tex @@ -22,6 +22,22 @@ \begin{enumerate}[label=\textbf{\arabic*}.] + +\item {[$\RHD$\textit{02}]} \textbf{Saturated subtraction} + +Implement the function + +\vspace{-1em} +\begin{alltt} + void monus(z_t r, z_t a, z_t b); +\end{alltt} +\vspace{-1em} + +\noindent +which calculates $a \dotminus b = \max \{ 0,~ a - b \}$. + + + \item {[\textit{M10}]} \textbf{Convergence of the Lucas Number ratios} Find an approximation for @@ -122,6 +138,20 @@ Use this to implement a fast primality tester. \begin{enumerate}[label=\textbf{\arabic*}.] +\item \textbf{Saturated subtraction} + +\vspace{-1em} +\begin{alltt} +void monus(z_t r, z_t a, z_t b) +\{ + zsub(r, a, b); + if (zsignum(r) < 0) + zsetu(r, 0); +\} +\end{alltt} + + + \item \textbf{Convergence of the Lucas Number ratios} It would be a mistake to use bignum, and bigint in particular, @@ -243,7 +273,7 @@ enum zprimality ptest_fast(z_t p) z_t a; int c = zcmpu(p, 2); if (c <= 0) - return c ? NONPRIME : PRIME; + return c ? NONPRIME : PRIME; zinit(a); zsetu(a, 1); zlsh(a, a, p); -- cgit v1.2.3-70-g09d2